Skip to content

Commit 0637d3a

Browse files
committed
no message
1 parent 5111543 commit 0637d3a

File tree

3 files changed

+117
-1
lines changed

3 files changed

+117
-1
lines changed

problems/binary-tree-paths.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,28 @@ def binaryTreePaths(self, root):
1515
if node.left: stack.append((node.left, path))
1616
if node.right: stack.append((node.right, path))
1717

18+
return ans
19+
20+
21+
"""
22+
Time: O(N)
23+
Space: O(N)
24+
25+
Standard BFS.
26+
"""
27+
class Solution(object):
28+
def binaryTreePaths(self, root):
29+
q = collections.deque([(root, '')])
30+
ans = []
31+
32+
while q:
33+
node, path = q.popleft()
34+
35+
path = (path+'->'+str(node.val)) if path else str(node.val)
36+
37+
if node.left: q.append((node.left, path))
38+
if node.right: q.append((node.right, path))
39+
40+
if not node.left and not node.right: ans.append(path)
41+
1842
return ans

problems/maximum-depth-of-binary-tree.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,28 @@ def maxDepth(self, root):
1818
if node.right:
1919
stack.append((node.right, depth+1))
2020
return max_depth
21-
21+
22+
23+
"""
24+
Time: O(N)
25+
Space: O(N)
26+
27+
Standard BFS.
28+
"""
29+
class Solution(object):
30+
def maxDepth(self, root):
31+
if not root: return 0
32+
33+
ans = float('-inf')
34+
q = collections.deque([(root, 1)])
35+
node = root
36+
37+
while q:
38+
node, d = q.popleft()
39+
40+
ans = max(ans, d)
41+
42+
if node.left: q.append((node.left, d+1))
43+
if node.right: q.append((node.right, d+1))
44+
45+
return ans
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
Time: O(NLogN)
3+
Space: O(N)
4+
"""
5+
class Solution(object):
6+
def recoverTree(self, root):
7+
memo = {} #{node.val:node}
8+
stack = []
9+
node = root
10+
vals = []
11+
12+
#inorder traversal and store the values in `vals` and `memo`
13+
while node or stack:
14+
while node:
15+
stack.append(node)
16+
node = node.left
17+
18+
node = stack.pop()
19+
20+
memo[node.val] = node
21+
vals.append(node.val)
22+
23+
node = node.right
24+
25+
#find two val that needed to be swapped
26+
diff = []
27+
sortedVals = sorted(vals)
28+
for i in xrange(len(sortedVals)):
29+
if vals[i]!=sortedVals[i]: diff.append(vals[i])
30+
if len(diff)>=2: break
31+
32+
#swap the values
33+
memo[diff[0]].val = diff[1]
34+
memo[diff[1]].val = diff[0]
35+
36+
return root
37+
38+
39+
"""
40+
Time: O(N)
41+
Space: O(N)
42+
"""
43+
class Solution(object):
44+
def recoverTree(self, root):
45+
stack = []
46+
node = root
47+
prev = TreeNode(float('-inf'))
48+
swap1 = swap2 = None
49+
50+
#inorder traversal and find the swapped values
51+
while node or stack:
52+
while node:
53+
stack.append(node)
54+
node = node.left
55+
56+
node = stack.pop()
57+
58+
if swap1==None and prev.val>node.val: swap1 = prev
59+
if swap1!=None and prev.val>node.val: swap2 = node
60+
61+
prev = node
62+
63+
node = node.right
64+
65+
#swap the values
66+
swap1.val, swap2.val = swap2.val, swap1.val
67+
68+
return root

0 commit comments

Comments
 (0)