-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSum Root to Leaf Numbers.py
53 lines (38 loc) · 1.14 KB
/
Sum Root to 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
"""
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3 which represents the number 123.
Find the total sum of all root-to-leaf numbers % 1003.
Example :
1
/ \
2 3
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Return the sum = (12 + 13) % 1003 = 25 % 1003 = 25.
"""
from Python.Level6.TreeDataStructure import Node
class Solution:
def sum_root_to_leaf(self, root):
path = ""
res = []
self.helper(path, res, root)
sum = 0
for i in res:
sum += int(i)
return sum
def helper(self, path, res, node):
if not node:
return res
if node.left is None and node.right is None:
res.append(path + str(node.data))
return res
path += str(node.data)
self.helper(path, res, node.left)
self.helper(path, res, node.right)
return res
s = Solution()
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(0)
print(s.sum_root_to_leaf(root))