Skip to content

Commit b55b96b

Browse files
committed
129
1 parent e7cbca2 commit b55b96b

3 files changed

+144
-4
lines changed

SUMMARY.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Summary
22

33
* [Introduction](README.md)
4+
* [leetcode 100 斩!回顾](leetcode100斩回顾.md)
45
* [1. Two Sum](leetCode-1-Two-Sum.md)
56
* [2. Add Two Numbers](leetCode-2-Add-Two-Numbers.md)
67
* [3. Longest Substring Without Repeating Characters](leetCode-3-Longest-Substring-Without-Repeating-Characters.md)
@@ -101,8 +102,7 @@
101102
* [98. Validate Binary Search Tree](leetCode-98-Validate-Binary-Search-Tree.md)
102103
* [99. Recover Binary Search Tree](leetcode-99-Recover-Binary-Search-Tree.md)
103104
* [100. Same Tree](leetcode-100-Same-Tree.md)
104-
* [leetcode 100 斩!回顾](leetcode100斩回顾.md)
105-
* [101 题到 128 题](leetcode-101-200.md)
105+
* [101 题到 129 题](leetcode-101-200.md)
106106
* [101. Symmetric Tree](leetcode-101-Symmetric-Tree.md)
107107
* [102. Binary Tree Level Order Traversal](leetcode-102-Binary-Tree-Level-Order-Traversal.md)
108108
* [103. Binary Tree Zigzag Level Order Traversal](leetcode-103-Binary-Tree-Zigzag-Level-Order-Traversal.md)
@@ -130,4 +130,5 @@
130130
* [125. Valid Palindrome](leetcode-125-Valid-Palindrome.md)
131131
* [126*. Word Ladder II](leetCode-126-Word-LadderII.md)
132132
* [127. Word Ladder](leetCode-127-Word-Ladder.md)
133-
* [128. Longest Consecutive Sequence](leetcode-128-Longest-Consecutive-Sequence.md)
133+
* [128. Longest Consecutive Sequence](leetcode-128-Longest-Consecutive-Sequence.md)
134+
* [129. Sum Root to Leaf Numbers](leetcode-129-Sum-Root-to-Leaf-Numbers.md)

leetcode-101-200.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,6 @@
5252

5353
<a href="leetCode-127-Word-Ladder.html">127. Word Ladder</a>
5454

55-
<a href="leetcode-128-Longest-Consecutive-Sequence.html">128. Longest Consecutive Sequence</a>
55+
<a href="leetcode-128-Longest-Consecutive-Sequence.html">128. Longest Consecutive Sequence</a>
56+
57+
<a href="leetcode-129-Sum-Root-to-Leaf-Numbers.html">129. Sum Root to Leaf Numbers</a>
+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# 题目描述(中等难度)
2+
3+
![](https://windliang.oss-cn-beijing.aliyuncs.com/129.jpg)
4+
5+
从根节点到叶子节点的路径组成一个数字,计算所有的数字和。
6+
7+
# 思路分析
8+
9+
[112 题](<https://leetcode.wang/leetcode-112-Path-Sum.html>) 有些像,112 题是给出一个 `sum`,然后去找这条路径。但本质上都一样的,只需要对二叉树进行遍历,遍历过程中记录当前路径的和就可以。说到遍历,无非就是 `BFS``DFS`,如果进行 `BFS`,过程中我们需要维护多条路径的和,所以我们选择 `DFS`
10+
11+
说到 `DFS` 的话,可以用递归,也可以用栈去实现,递归会好理解一些,所以这里就只介绍递归吧,栈的话前边也用过很多了,可以看下 [112 题](<https://leetcode.wang/leetcode-112-Path-Sum.html>)
12+
13+
说到递归,既可以利用回溯的思想,也可以用分治的思想,这里就用这两种方式写一下,关于回溯、分治,可以看一下 [115 题](<https://leetcode.wang/leetcode-115-Distinct-Subsequences.html>),会有一个深刻的理解。
14+
15+
# 解法一 回溯法
16+
17+
回溯的思想就是一直进行深度遍历,直到得到一个解后,记录当前解。然后再回到之前的状态继续进行深度遍历。
18+
19+
所以我们需要定义一个函数来得到这个解。
20+
21+
```java
22+
void dfs(TreeNode root, int cursum)
23+
```
24+
25+
这个函数表示从根节点走到 `root` 节点的时候,路径累积的和是 `cursum`
26+
27+
这里我们用一个全局变量 `sum` 来保存每条路径的和。
28+
29+
所以回溯的出口就是,当我们到达叶子节点,保存当前累计的路径和。
30+
31+
```java
32+
private void dfs(TreeNode root, int cursum) {
33+
if (root.left == null && root.right == null) {
34+
sum += cursum;
35+
return;
36+
}
37+
```
38+
39+
然后就是分别去尝试左子树和右子树就可以。把所有的代码合起来。
40+
41+
```java
42+
public int sumNumbers(TreeNode root) {
43+
if (root == null) {
44+
return 0;
45+
}
46+
dfs(root, root.val);
47+
return sum;
48+
}
49+
50+
int sum = 0;
51+
52+
private void dfs(TreeNode root, int cursum) {
53+
//到达叶子节点
54+
if (root.left == null && root.right == null) {
55+
sum += cursum;
56+
return;
57+
}
58+
//尝试左子树
59+
if(root.left!=null){
60+
dfs(root.left, cursum * 10 + root.left.val);
61+
}
62+
//尝试右子树
63+
if(root.right!=null){
64+
dfs(root.right, cursum * 10 + root.right.val);
65+
66+
}
67+
68+
}
69+
70+
```
71+
72+
# 解法二 分治法
73+
74+
分支法的思想就是,解决子问题,通过子问题解决最终问题。
75+
76+
要求一个树所有的路径和,我们只需要知道从根节点出发经过左子树的所有路径和和从根节点出发经过右子树的所有路径和,加起来就可以了。
77+
78+
所以我们需要定义一个函数。
79+
80+
```java
81+
int sumNumbersHelper(TreeNode root, int sum)
82+
```
83+
84+
参数含义是经过当前 `root` 节点之前,已经累计的和是 `sum`,函数返回从最初根节点经过当前 `root` 节点达到叶子节点的和。(明确函数的定义很重要,这样才可以保证正确的写出递归)
85+
86+
所以如果经过当前节点,那么当前已有路径的和就是
87+
88+
```java
89+
int cursum = sum * 10 + root.val;
90+
```
91+
92+
然后我们需要考虑经过当前 `root` 节点后,再经过它的左孩子到叶子节点的所有路径和。
93+
94+
```java
95+
int ans1 = sumNumbersHelper(root.left,cursum)
96+
```
97+
98+
再考虑经过当前 `root` 节点后,再经过它的右孩子到叶子节点的路径和。
99+
100+
```java
101+
int ans2 = sumNumbersHelper(root.right,cursum)
102+
```
103+
104+
两个都算出来以后,加起来就是从最初根节点经过当前 `root` 节点到达叶子节点的所有路径和了。
105+
106+
```java
107+
public int sumNumbers(TreeNode root) {
108+
if (root == null) {
109+
return 0;
110+
}
111+
return sumNumbersHelper(root, 0);
112+
}
113+
114+
private int sumNumbersHelper(TreeNode root, int sum) {
115+
//已经累计的和
116+
int cursum = sum * 10 + root.val;
117+
if (root.left == null && root.right == null) {
118+
return cursum;
119+
}
120+
int ans = 0;
121+
//从最开始经过当前 root 再经过左孩子到达叶子节点所有的路径和
122+
if (root.left != null) {
123+
ans += sumNumbersHelper(root.left, cursum);
124+
}
125+
//从最开始经过当前 root 再经过右孩子到达叶子节点所有的路径和
126+
if (root.right != null) {
127+
ans += sumNumbersHelper(root.right, cursum);
128+
}
129+
//返回从最开始经过当前 root 然后到达叶子节点所有的路径和
130+
return ans;
131+
}
132+
133+
```
134+
135+
# 总
136+
137+
这道题本质上还是在考二叉树的遍历,回溯和分治的思想的区别也可以对比考虑一下。

0 commit comments

Comments
 (0)