-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathPath Sum.py
59 lines (42 loc) · 1.28 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
"""
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along
the path equals the given sum.
Example :
Given the below binary tree and sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
Return 0 / 1 ( 0 for false, 1 for true ) for this problem
"""
from Python.Level6.TreeDataStructure import Node
class Solution:
def path_sum(self, root, target):
if root is None:
return target == 0
else:
res = 0
target -= root.data
if target == 0 and root.left is None and root.right is None:
return 1
if root.left:
res = res or self.path_sum(root.left, target)
if root.right:
res = res or self.path_sum(root.right, target)
return res
"""Testing program"""
s = Solution()
root = Node(5)
root.left = Node(4)
root.right = Node(8)
root.left.left = Node(11)
root.right.left = Node(13)
root.right.right = Node(4)
root.left.left.left = Node(7)
root.left.left.right = Node(2)
root.right.right.right = Node(1)
print(s.path_sum(root, 22))