-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path129.py
38 lines (31 loc) · 855 Bytes
/
129.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
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def __init__(self):
self.sum = 0
def dfs(self, root, tillNow):
if root is None:
return
self.dfs(root.left, tillNow*10+root.val)
if root.left is None and root.right is None:
self.sum +=(tillNow*10)+root.val
self.dfs(root.right, tillNow * 10 + root.val)
def sumNumbers(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.dfs(root,0)
return self.sum
if __name__=="__main__":
s = Solution()
t = TreeNode(4)
t.left = TreeNode(9)
t.right = TreeNode(0)
t.left.left = TreeNode(5)
t.left.right = TreeNode(1)
print(s.sumNumbers(t))