-
Notifications
You must be signed in to change notification settings - Fork 17
/
path-sum.py
59 lines (47 loc) · 1.81 KB
/
path-sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def hasPathSumDFS(self, root, summ):
if root is None:
return False
stack = [(root, root.val)]
# Assign placeholder to last_removed to pass first iteration
last_removed = TreeNode(None)
last_removed.right = TreeNode(None)
while stack:
node, node_val = stack[-1]
if not node.left and not node.right and node_val == summ:
return True
# Check if
# 1. This node has no left child
# 2. Last removed node was the child of this node
# 3. Last removed was the rightmost member of the left subtree of \
# this node
if not node.left or (node.left == last_removed or last_removed.right == None):
last_removed, last_removed_val = stack.pop()
if node.right:
stack.append((node.right, node_val + node.right.val))
else:
stack.append((node.left, node_val + node.left.val))
return False
def hasPathSumBFS(self, root, summ):
if root is None:
return False
stack = [(root, root.val)]
while stack:
old_stack = stack
stack = []
for node, node_val in old_stack:
if node.left or node.right:
if node.left:
stack.append((node.left, node_val + node.left.val))
if node.right:
stack.append((node.right, node_val + node.right.val))
else:
if node_val == summ:
return True
return False