Skip to content

Commit 82df772

Browse files
committed
finish "129. Sum Root to Leaf Numbers". Fix #129
1 parent c0676a7 commit 82df772

File tree

4 files changed

+79
-6
lines changed

4 files changed

+79
-6
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -777,12 +777,12 @@
777777
//|{source_base_url}/_0128_LongestConsecutiveSequence.java[Java]
778778
//|{doc_base_url}/0128-longest-consecutive-sequence.adoc[Note]
779779
//|Hard
780-
//
781-
//|129
782-
//|{leetcode_base_url}/sum-root-to-leaf-numbers/[Sum Root to Leaf Numbers]
783-
//|{source_base_url}/_0129_SumRootToLeafNumbers.java[Java]
784-
//|{doc_base_url}/0129-sum-root-to-leaf-numbers.adoc[Note]
785-
//|Medium
780+
781+
|129
782+
|{leetcode_base_url}/sum-root-to-leaf-numbers/[Sum Root to Leaf Numbers]
783+
|{source_base_url}/_0129_SumRootToLeafNumbers.java[Java]
784+
|{doc_base_url}/0129-sum-root-to-leaf-numbers.adoc[Note]
785+
|Medium
786786

787787
|130
788788
|{leetcode_base_url}/surrounded-regions/[Surrounded Regions]

docs/0112-path-sum.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ return true, as there exist a root-to-leaf path `5->4->11->2` which sum is 22.
2828
减去当前节点值的只,然后递归调用,到叶子节点和目标值相等即可。
2929

3030
注意把代码写简化点!
31+
32+
这道题和 xref:0129-sum-root-to-leaf-numbers.adoc[129. Sum Root to Leaf Numbers] 类似。
33+

docs/0129-sum-root-to-leaf-numbers.adoc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,18 @@ The root-to-leaf path `4->0` represents the number 40.
4343
Therefore, sum = 495 + 491 + 40 = `1026`.
4444
----
4545

46+
=== 解题分析
47+
48+
这道题和 xref:0112-path-sum.adoc[112. Path Sum] 类似。
49+
50+
找到所有路径,父级乘以10,再加当前节点,最后到叶子节点时,加入到总和中。
51+
52+
=== 思考题
53+
54+
. 尝试使用中根遍历和后跟遍历的解法。
55+
. 目前解法是会用了实例变量,能否取去掉?
56+
57+
=== 参考资料
58+
59+
. https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/solution/3zhong-jie-fa-di-gui-xian-xu-fei-di-gui-xian-xu-ce/[3中解法:递归先序、非递归先序、层序 - 求根到叶子节点数字之和 - 力扣(LeetCode)]
60+
. https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by-3-5/[详细通俗的思路分析,多解法 - 求根到叶子节点数字之和 - 力扣(LeetCode)]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.diguage.algorithm.leetcode;
2+
3+
import com.diguage.algorithm.util.TreeNode;
4+
5+
import java.util.Objects;
6+
7+
import static com.diguage.algorithm.util.TreeNodeUtils.buildTree;
8+
import static java.util.Arrays.asList;
9+
10+
/**
11+
* = 129. Sum Root to Leaf Numbers
12+
*
13+
* https://leetcode.com/problems/sum-root-to-leaf-numbers/[Sum Root to Leaf Numbers - LeetCode]
14+
*
15+
* @author D瓜哥, https://www.diguage.com/
16+
* @since 2020-02-07 23:56
17+
*/
18+
public class _0129_SumRootToLeafNumbers {
19+
20+
/**
21+
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Sum Root to Leaf Numbers.
22+
* Memory Usage: 37.7 MB, less than 5.13% of Java online submissions for Sum Root to Leaf Numbers.
23+
*/
24+
private int sum = 0;
25+
public int sumNumbers(TreeNode root) {
26+
dfs(root, 0);
27+
return sum;
28+
}
29+
30+
private void dfs(TreeNode root, int parent) {
31+
if (Objects.isNull(root)) {
32+
return;
33+
}
34+
int temp = parent * 10 + root.val;
35+
if (Objects.isNull(root.left) && Objects.isNull(root.right)) {
36+
sum += temp;
37+
return;
38+
}
39+
dfs(root.left, temp);
40+
dfs(root.right, temp);
41+
}
42+
43+
44+
public static void main(String[] args) {
45+
_0129_SumRootToLeafNumbers solution = new _0129_SumRootToLeafNumbers();
46+
47+
solution.sum = 0;
48+
int r1 = solution.sumNumbers(buildTree(asList(1, 2, 3)));
49+
System.out.println((r1 == 25) + " : " + r1);
50+
51+
solution.sum = 0;
52+
int r2 = solution.sumNumbers(buildTree(asList(4, 9, 0, 5, 1)));
53+
System.out.println((r2 == 1026) + " : " + r2);
54+
}
55+
}

0 commit comments

Comments
 (0)