-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrootToLeafSum.py
More file actions
59 lines (38 loc) · 1.06 KB
/
rootToLeafSum.py
File metadata and controls
59 lines (38 loc) · 1.06 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
__author__ = 'kathan'
class Node():
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def find_sum(node, total, current_list):
if total < 0:
return False
total = total - node.value
current_list.append(node.value)
if not node.left and not node.right and total == 0:
return True
if node.left and find_sum(node.left, total, current_list):
return True
if node.right and find_sum(node.right, total, current_list):
return True
current_list.pop()
return False
root = Node(15)
root.left = Node(10)
root.right = Node(20)
root.left.left = Node(6)
root.left.right = Node(7)
root.left.right.left = Node(2)
root.left.right.right = Node(16)
root.left.right.left.left = Node(1)
root.left.right.left.right = Node(5)
root.left.right.right.left = Node(3)
root.right.left = Node(22)
# 15
# 10 20
# 6 7 22
# 2 16
# 1 5 3
lst = []
print find_sum(root, 35, lst)
print lst