forked from roseperrone/interview-questions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_tree_sum.py
executable file
·43 lines (32 loc) · 1.01 KB
/
find_tree_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
#!/usr/bin/python
class Node(object):
def __init__(self, data, left, right):
self.data = data
self.left = left
self.right = right
def all_paths_that_sum_to(target, head, path):
'''
Returns all paths thaht sum up to the target, either
continuing the path, or beginning at a new node at head or below.
'''
if target < 0 or head is None:
return []
result = []
path_sum = 0
for node in path:
path_sum += node.data
intermediate_target = target - head.data
if intermediate_target == 0:
# paths not starting from the current node
result.extend(all_paths_that_sum_to(target, head.right, [])
result.extend(all_paths_that_sum_to(target, head.left, [])
# paths continuing the current path
path.append(head)
if path_sum + head.data == target:
result.append(head)
else:
result.extend(all_paths_that_sum_to(target, head.left, path)
result.extend(all_paths_that_sum_to(target, head.right, path)
return result
tree = something
paths = all_paths_that_sum_to(21, tree, [])