Skip to content

Commit 9f00706

Browse files
committed
no message
1 parent 5e5e2ff commit 9f00706

File tree

3 files changed

+139
-1
lines changed

3 files changed

+139
-1
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Time: O(N)
3+
Space: O(1)
4+
5+
View each level as a link list.
6+
Traverse the tree level by level. For each level:
7+
Starting from the leftmost (the head of the link list), we establish the next pointer of the children.
8+
9+
curr.left.next = curr.right or (curr.next.left or curr.next.right) or (curr.next.next.left or curr.next.next.right) or (...)
10+
curr.right.next = (curr.next.left or curr.next.right) or (curr.next.next.left or curr.next.next.right) or (...)
11+
"""
12+
class Solution(object):
13+
def connect(self, root):
14+
if not root: return root
15+
16+
leftmost = root
17+
18+
while leftmost:
19+
curr = leftmost
20+
21+
while curr:
22+
if curr.left:
23+
if curr.right:
24+
curr.left.next = curr.right
25+
else:
26+
n = curr.next
27+
while n:
28+
curr.left.next = n.left or n.right
29+
if curr.left.next: break
30+
n = n.next
31+
32+
if curr.right:
33+
n = curr.next
34+
while n:
35+
curr.right.next = n.left or n.right
36+
if curr.right.next: break
37+
n = n.next
38+
39+
curr = curr.next
40+
41+
nextLevelLeftmost = None
42+
n = leftmost
43+
while n:
44+
if n.left or n.right:
45+
nextLevelLeftmost = n.left or n.right
46+
break
47+
n = n.next
48+
leftmost = nextLevelLeftmost
49+
50+
return root
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
Time: O(N)
3+
Space: O(N)
4+
5+
Traverse the tree level by level.
6+
For each level connect the node to the "next node".
7+
"""
8+
class Solution(object):
9+
def connect(self, root):
10+
if not root: return root
11+
level = collections.deque([root])
12+
nextLevel = collections.deque()
13+
14+
while level:
15+
node = level.popleft()
16+
17+
if node.left: nextLevel.append(node.left)
18+
if node.right: nextLevel.append(node.right)
19+
20+
if not level:
21+
if not nextLevel: break
22+
for i, c in enumerate(nextLevel):
23+
if i==len(nextLevel)-1: continue #skip last
24+
c.next = nextLevel[i+1]
25+
level = nextLevel
26+
nextLevel = collections.deque()
27+
28+
return root
29+
30+
31+
"""
32+
Time: O(N)
33+
Space: O(1)
34+
35+
View each level as a link list.
36+
Traverse the tree level by level. For each level:
37+
Starting from the leftmost (the head of the link list), we establish the next pointer of the children.
38+
curr.left.next = curr.right
39+
curr.right.next = curr.next.left
40+
"""
41+
class Solution(object):
42+
def connect(self, root):
43+
if not root: return root
44+
45+
leftmost = root
46+
while leftmost:
47+
48+
curr = leftmost
49+
while curr:
50+
if curr.left: curr.left.next = curr.right
51+
if curr.right and curr.next: curr.right.next = curr.next.left
52+
curr = curr.next
53+
54+
leftmost = leftmost.left
55+
56+
return root

problems/unique-binary-search-trees.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,36 @@ def numTrees(self, n):
1111
for root in xrange(1, i+1):
1212
dp[i] += dp[root-1]*dp[i-root]
1313

14-
return dp[n]
14+
return dp[n]
15+
16+
"""
17+
dp[n] := the number of structurally unique BST's which has n nodes with val 1~n
18+
dp[n] = helper(n)
19+
20+
Form 1~n nodes
21+
If we use 1 as root, the left subtree possible count will be dp[1-1] and right subtree be dp[n-1], the count will be dp[0]*dp[n-1].
22+
If we use 2 as root, the left subtree possible count will be dp[2-1] and right subtree be dp[n-2], the count will be dp[1]*dp[n-2].
23+
If we use 3 as root, the left subtree possible count will be dp[3-1] and right subtree be dp[n-3], the count will be dp[2]*dp[n-3].
24+
...
25+
If we use i as root, the left subtree possible count will be dp[i-1] and right subtree be dp[n-i], the count will be dp[i-1]*dp[n-i].
26+
27+
So the number of structurally unique BST's which has n nodes with val 1~n, will be the sum above.
28+
29+
Time: O(N^2)
30+
Space: O(N)
31+
"""
32+
class Solution(object):
33+
def numTrees(self, N):
34+
def helper(n):
35+
count = 0
36+
for i in xrange(1, n+1):
37+
count += dp[i-1]*dp[n-i]
38+
return count
39+
40+
dp = [0]*(N+1)
41+
dp[0] = 1
42+
dp[1] = 1
43+
44+
for n in xrange(2, N+1):
45+
dp[n] = helper(n)
46+
return dp[N]

0 commit comments

Comments
 (0)