LeetCode_Sum Root to Leaf Numbers

Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers.
(树型数值之和)

Note: A leaf is a node with no children.

Example:



1. DFS

常规的 DFS 算法,具体实现过程如下:

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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
def dfs(self, root):
if not root:
return

left = self.dfs(root.left)
right = self.dfs(root.right)

if not left and not right:
return str(root.val)

result = []
if left:
for num in left:
result.append(str(root.val)+str(num))
if right:
for num in right:
result.append(str(root.val)+str(num))

return result

def sumNumbers(self, root: TreeNode) -> int:
if not root:
return 0

nums = self.dfs(root)
return sum(int(num) for num in nums)