-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path061_sumNumbers.py
More file actions
33 lines (29 loc) · 982 Bytes
/
061_sumNumbers.py
File metadata and controls
33 lines (29 loc) · 982 Bytes
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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def sumNumbers(self, root: TreeNode) -> int:
self.tempStr = ""
array = []
def _helper(root):
if root == None:
return None
if root.left == None and root.right == None:
array.append(self.tempStr + str(root.val))
return self.tempStr
if root.left != None:
self.tempStr += str(root.val)
elif root.right != None:
self.tempStr += str(root.val)
left = _helper(root.left)
right = _helper(root.right)
self.tempStr = self.tempStr[:-1]
return self.tempStr
_helper(root)
totSum = 0
for num in array:
totSum += int(num)
return totSum