-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path129.py
More file actions
24 lines (23 loc) · 693 Bytes
/
129.py
File metadata and controls
24 lines (23 loc) · 693 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
class Solution(object):
def sumNumbers(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return 0
ans = []
def dfs(node, path):
if node:
path.append(node.val)
if not node.left and not node.right:
strpath = [str(i) for i in path]
val = int("".join(strpath))
ans.append(val)
if node.left:
dfs(node.left, path)
if node.right:
dfs(node.right, path)
path.pop()
dfs(root, [])
return sum(ans)