Skip to content

Commit 85a1dea

Browse files
committed
no message
1 parent c9e8b16 commit 85a1dea

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
Time: O(N)
3+
Space: O(N)
4+
5+
Pre-Order Traversal
6+
"""
7+
class Solution(object):
8+
def flatten(self, root):
9+
if not root: return None
10+
11+
preHead = TreeNode(0)
12+
curr = preHead
13+
stack = [root]
14+
15+
while stack:
16+
node = stack.pop()
17+
18+
if node.right: stack.append(node.right)
19+
if node.left: stack.append(node.left)
20+
21+
node.right = None
22+
node.left = None
23+
curr.right = node
24+
curr = curr.right
25+
26+
return preHead.right
27+
28+
"""
29+
Time: O(N)
30+
Space: O(1)
31+
32+
Morris Traversal
33+
"""
34+
class Solution(object):
35+
def flatten(self, root):
36+
node = root
37+
38+
while node:
39+
if node.left:
40+
rightmost = node.left
41+
while rightmost.right: rightmost = rightmost.right
42+
rightmost.right = node.right
43+
node.right = node.left
44+
node.left = None
45+
else:
46+
node = node.right
47+
return root

problems/path-sum-ii.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,26 @@ def pathSum(self, root, S):
1717
"""
1818
Time complexity is O(N), because we traverse all the nodes.
1919
Space complexity is O(N^2), because in the worst case, all node could carry all the other nodes in the `path`.
20-
"""
20+
"""
21+
22+
23+
24+
"""
25+
Time complexity is O(N), because we traverse all the nodes.
26+
Space complexity is O(N^2), because in the worst case, all node could carry all the other nodes in the `path`.
27+
"""
28+
class Solution(object):
29+
def pathSum(self, root, targetSum):
30+
if not root: return []
31+
32+
ans = []
33+
stack = [(root, root.val, [root.val])]
34+
35+
while stack:
36+
node, total, path = stack.pop()
37+
38+
if not node.left and not node.right and total==targetSum: ans.append(path)
39+
if node.left: stack.append((node.left, total+node.left.val, path+[node.left.val]))
40+
if node.right: stack.append((node.right, total+node.right.val, path+[node.right.val]))
41+
42+
return ans

problems/path-sum.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,27 @@ def hasPathSum(self, root, S):
1111
if not node.left and not node.right and s==S: return True
1212
if node.left: stack.append((node.left, s))
1313
if node.right: stack.append((node.right, s))
14-
return False
14+
return False
15+
16+
17+
"""
18+
Time: O(N)
19+
Space: O(N)
20+
21+
DFS
22+
"""
23+
class Solution(object):
24+
def hasPathSum(self, root, targetSum):
25+
if not root: return False
26+
27+
stack = [(root, root.val)]
28+
29+
while stack:
30+
node, total = stack.pop()
31+
32+
if not node.left and not node.right and total==targetSum: return True
33+
if node.left: stack.append((node.left, total+node.left.val))
34+
if node.right: stack.append((node.right, total+node.right.val))
35+
36+
return False
37+

0 commit comments

Comments
 (0)