-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_129.py
More file actions
36 lines (33 loc) · 1.03 KB
/
leetcode_129.py
File metadata and controls
36 lines (33 loc) · 1.03 KB
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
# 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:
result = self.binaryTreePaths(root)
total_sum = 0
for item in result:
item = item.lstrip("0")
if len(item) == 0:
item = 0
else:
item = int(item)
total_sum = total_sum + item
return total_sum
def binaryTreePaths(self, root):
# write your code here
if root == None:
return []
res = []
if root.left == None and root.right == None:
res.append(str(root.val))
return res
left_res = self.binaryTreePaths(root.left)
right_res = self.binaryTreePaths(root.right)
for item in left_res:
res.append(str(root.val) + item)
for item in right_res:
res.append(str(root.val) + item)
return res