mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
添加0129求根到叶子节点数字之和 Python3版本
This commit is contained in:
@ -165,7 +165,32 @@ public:
|
|||||||
Java:
|
Java:
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python3
|
||||||
|
class Solution:
|
||||||
|
def sumNumbers(self, root: TreeNode) -> int:
|
||||||
|
res = 0
|
||||||
|
path = []
|
||||||
|
def backtrace(root):
|
||||||
|
nonlocal res
|
||||||
|
if not root: return # 节点空则返回
|
||||||
|
path.append(root.val)
|
||||||
|
if not root.left and not root.right: # 遇到了叶子节点
|
||||||
|
res += get_sum(path)
|
||||||
|
if root.left: # 左子树不空
|
||||||
|
backtrace(root.left)
|
||||||
|
if root.right: # 右子树不空
|
||||||
|
backtrace(root.right)
|
||||||
|
path.pop()
|
||||||
|
|
||||||
|
def get_sum(arr):
|
||||||
|
s = 0
|
||||||
|
for i in range(len(arr)):
|
||||||
|
s = s * 10 + arr[i]
|
||||||
|
return s
|
||||||
|
|
||||||
|
backtrace(root)
|
||||||
|
return res
|
||||||
|
```
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
JavaScript:
|
JavaScript:
|
||||||
|
Reference in New Issue
Block a user