File tree Expand file tree Collapse file tree 4 files changed +162
-0
lines changed Expand file tree Collapse file tree 4 files changed +162
-0
lines changed Original file line number Diff line number Diff line change
1
+ '''
2
+ Given a binary tree
3
+
4
+ struct TreeLinkNode {
5
+ TreeLinkNode *left;
6
+ TreeLinkNode *right;
7
+ TreeLinkNode *next;
8
+ }
9
+ 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.
10
+
11
+ Initially, all next pointers are set to NULL
12
+ Example:
13
+
14
+ Given the following perfect binary tree,
15
+
16
+ 1
17
+ / \
18
+ 2 3
19
+ / \ / \
20
+ 4 5 6 7
21
+ After calling your function, the tree should look like:
22
+
23
+ 1 -> NULL
24
+ / \
25
+ 2 -> 3 -> NULL
26
+ / \ / \
27
+ 4->5->6->7 -> NULL
28
+ '''
29
+
30
+ # Definition for binary tree with next pointer.
31
+ # class TreeLinkNode:
32
+ # def __init__(self, x):
33
+ # self.val = x
34
+ # self.left = None
35
+ # self.right = None
36
+ # self.next = None
37
+
38
+ class Solution :
39
+ # @param root, a tree link node
40
+ # @return nothing
41
+ def connect (self , root ):
42
+ def recursive (node ):
43
+ if node is None :
44
+ return
45
+
46
+ if node .left :
47
+ node .left .next = node .right
48
+ if node .right :
49
+ if node .next :
50
+ node .right .next = node .next .left
51
+ else :
52
+ node .right .next = None
53
+ recursive (node .left )
54
+ recursive (node .right )
55
+
56
+ if root != None :
57
+ root .next = None
58
+ recursive (root )
Original file line number Diff line number Diff line change
1
+ '''
2
+ Given a binary tree
3
+
4
+ struct TreeLinkNode {
5
+ TreeLinkNode *left;
6
+ TreeLinkNode *right;
7
+ TreeLinkNode *next;
8
+ }
9
+ 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.
10
+
11
+ Initially, all next pointers are set to NULL.
12
+
13
+ Example:
14
+
15
+ Given the following binary tree,
16
+
17
+ 1
18
+ / \
19
+ 2 3
20
+ / \ \
21
+ 4 5 7
22
+ After calling your function, the tree should look like:
23
+
24
+ 1 -> NULL
25
+ / \
26
+ 2 -> 3 -> NULL
27
+ / \ \
28
+ 4-> 5 -> 7 -> NULL
29
+ '''
30
+
31
+ # Definition for binary tree with next pointer.
32
+ # class TreeLinkNode:
33
+ # def __init__(self, x):
34
+ # self.val = x
35
+ # self.left = None
36
+ # self.right = None
37
+ # self.next = None
38
+
39
+ class Solution :
40
+ # @param root, a tree link node
41
+ # @return nothing
42
+ def connect (self , root ):
43
+ if root == None :
44
+ return
45
+ queue = [root ]
46
+ queue .append (None )
47
+
48
+ while queue :
49
+ front = queue .pop (0 )
50
+ if front is not None :
51
+ front .next = queue [0 ]
52
+ if front .left :
53
+ queue .append (front .left )
54
+ if front .right :
55
+ queue .append (front .right )
56
+ elif queue :
57
+ queue .append (None )
Original file line number Diff line number Diff line change
1
+ '''
2
+ Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
3
+
4
+ Example:
5
+
6
+ Input: 5
7
+ Output:
8
+ [
9
+ [1],
10
+ [1,1],
11
+ [1,2,1],
12
+ [1,3,3,1],
13
+ [1,4,6,4,1]
14
+ ]
15
+ '''
16
+ class Solution (object ):
17
+ def generate (self , numRows ):
18
+ """
19
+ :type numRows: int
20
+ :rtype: List[List[int]]
21
+ """
22
+ triangle = []
23
+
24
+ for row in range (numRows ):
25
+ new_row = [0 for _ in range (row + 1 )]
26
+ new_row [0 ], new_row [- 1 ] = 1 , 1
27
+
28
+ for col in range (1 , len (new_row )- 1 ):
29
+ new_row [col ] = triangle [row - 1 ][col - 1 ] + triangle [row - 1 ][col ]
30
+ triangle .append (new_row )
31
+ return triangle
Original file line number Diff line number Diff line change
1
+ '''
2
+ Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.
3
+
4
+ Note that the row index starts from 0.
5
+ '''
6
+ class Solution (object ):
7
+ def getRow (self , rowIndex ):
8
+ """
9
+ :type rowIndex: int
10
+ :rtype: List[int]
11
+ """
12
+ row = [1 ]* (rowIndex + 1 )
13
+ for i in range (1 , rowIndex + 1 ):
14
+ for j in range (i - 1 , 0 , - 1 ):
15
+ row [j ] += row [j - 1 ]
16
+ return row
You can’t perform that action at this time.
0 commit comments