forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0129.py
37 lines (32 loc) · 1.02 KB
/
0129.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
class Solution:
def sumNumbers(self, root):
"""
:type root: TreeNode
:rtype: int
"""
result = 0
if not root:
return result
path = [(0, root)]
while path:
pre, node = path.pop()
if node:
if not node.left and not node.right :
result += pre*10 + node.val
path += [(pre*10 + node.val, node.right), (pre*10 + node.val, node.left)]
return result
# class Solution:
# def _sumNumbers(self, root, value):
# if root:
# self._sumNumbers(root.left, value*10+root.val)
# self._sumNumbers(root.right, value*10+root.val)
# if not root.left and not root.right:
# self.result += value*10 + root.val
# def sumNumbers(self, root):
# """
# :type root: TreeNode
# :rtype: int
# """
# self.result = 0
# self._sumNumbers(root, 0)
# return self.result