https://leetcode.com/problems/pascals-triangle/ 118. Pascal’s Triangle Total Accepted: 103611 Total Submissions: 290013 Difficulty: Easy Contributors: Admin Given numRows, generate the first numRows of Pascal’s triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] Code class Solution(object): def generate(self, numRows): “”” :type numRows: int :rtype: List[List[int]] “”” if numRows == 0: return …
Monthly Archives: October 2016
Leetcode 120: Triangle
120. Triangle Total Accepted: 84503 Total Submissions: 265352 Difficulty: Medium Contributors: Admin Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to …
Leetcode 116: Populating Next Right Pointers in Each Node
116. Populating Next Right Pointers in Each Node Total Submissions: 287603 Difficulty: Medium Contributors: Admin Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. …
Continue reading “Leetcode 116: Populating Next Right Pointers in Each Node”
Leetcode 222: Count Complete Tree Nodes
222. Count Complete Tree Nodes Total Accepted: 45962 Total Submissions: 173085 Difficulty: Medium Contributors: Admin Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as …
Leetcode 114: Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 Code # Definition for a binary tree node. # class TreeNode(object): # def …
Continue reading “Leetcode 114: Flatten Binary Tree to Linked List”
Leetcode 331: Verify Preorder Serialization of a Binary Tree
331. Verify Preorder Serialization of a Binary Tree Total Accepted: 23475 Total Submissions: 68739 Difficulty: Medium Contributors: Admin One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node’s value. If it is a null node, we record using a sentinel value such as …
Continue reading “Leetcode 331: Verify Preorder Serialization of a Binary Tree”
Leetcode 104: Maximum Depth of Binary Tree
104. Maximum Depth of Binary Tree Total Accepted: 184446 Total Submissions: 369369 Difficulty: Easy Contributors: Admin Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Code (BFS) from collections import * # Definition …
Continue reading “Leetcode 104: Maximum Depth of Binary Tree”
Leetcode 378: Kth Smallest Element in a Sorted Matrix
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not the kth distinct element. Example: matrix = [ [ 1, 5, 9], [10, 11, 13], [12, …
Continue reading “Leetcode 378: Kth Smallest Element in a Sorted Matrix”
Leetcode 9: Palindrome Number
https://leetcode.com/problems/palindrome-number/ 9. Palindrome Number Total Accepted: 156158 Total Submissions: 467127 Difficulty: Easy Contributors: Admin Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using …
199: Binary Tree Right Side View
199. Binary Tree Right Side View Total Accepted: 56659 Total Submissions: 151351 Difficulty: Medium Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example:Given the following binary tree, 1 <— / \ 2 3 <— \ …