-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm129.py
29 lines (25 loc) · 917 Bytes
/
m129.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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
self.summ = 0
def helper(current: Optional[TreeNode], currSum: []) -> None :
if not current :
print(int(''.join(currSum)))
self.summ += int(''.join(currSum))
return
currSum.append(str(current.val))
if current.left and current.right :
helper(current.left, currSum)
helper(current.right, currSum)
elif current.left :
helper(current.left, currSum)
else :
helper(current.right, currSum)
currSum.pop()
helper(root, [])
return self.summ