-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhasPathSum
More file actions
38 lines (33 loc) · 1.03 KB
/
hasPathSum
File metadata and controls
38 lines (33 loc) · 1.03 KB
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
# Definition for a binary tree node.
class node:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def hasPathSum(self, root, target):
result = False
def dfs(node, currSum):
# using nonlocal...first for me
nonlocal result
if not node:
return
print(node.val)
currSum += node.val
if currSum == target:
result = True
dfs(node.left, currSum)
dfs(node.right, currSum)
dfs(root, 0)
return result
root = node(5)
root.left = node(4)
root.right = node(8)
root.left.left = node(11)
root.left.left.left = node(7)
root.left.left.right = node(2)
root.right.left = node(13)
root.right.right = node(4)
root.right.right.right = node(1)
myVar = Solution()
myVar.hasPathSum(root, 22)