-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsum_root_leaf_numbers.py
58 lines (39 loc) · 1.13 KB
/
sum_root_leaf_numbers.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
class TreeNode():
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def __repr__(self):
return '<TreeNode {}>'.format(self.val)
def sum_root_leaf_paths(root):
paths = root_leaf_paths(root, [])
def number_from_path(p):
digits = [str(node.val) for node in p]
return int(''.join(digits))
numbers = [number_from_path(p) for p in paths]
return sum(numbers) % 1003
def root_leaf_paths(node, path):
if node is None:
return
if node.left is None and node.right is None:
yield path + [node]
for p in root_leaf_paths(node.left, path + [node]):
yield p
for p in root_leaf_paths(node.right, path + [node]):
yield p
def test_sum_root_leaf_paths():
n1 = TreeNode(1)
n2 = TreeNode(2)
n3 = TreeNode(3)
n4 = TreeNode(4)
n5 = TreeNode(5)
n6 = TreeNode(6)
n4.left = n2
n4.right = n6
n2.left = n1
n2.right = n3
n6.left = n5
root = n4
assert sum_root_leaf_paths(root) == 306
def test_sum_root_leaf_paths_single_node():
assert sum_root_leaf_paths(TreeNode(3)) == 3