Skip to content

Commit c99d7de

Browse files
committed
一刷1022
1 parent 8a8c6b2 commit c99d7de

File tree

7 files changed

+100
-28
lines changed

7 files changed

+100
-28
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7179,14 +7179,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
71797179
//|{doc_base_url}/1021-remove-outermost-parentheses.adoc[题解]
71807180
//|Easy
71817181
//|
7182-
//
7183-
//|{counter:codes}
7184-
//|{leetcode_base_url}/sum-of-root-to-leaf-binary-numbers/[1022. Sum of Root To Leaf Binary Numbers^]
7185-
//|{source_base_url}/_1022_SumOfRootToLeafBinaryNumbers.java[Java]
7186-
//|{doc_base_url}/1022-sum-of-root-to-leaf-binary-numbers.adoc[题解]
7187-
//|Easy
7188-
//|
7189-
//
7182+
7183+
|{counter:codes}
7184+
|{leetcode_base_url}/sum-of-root-to-leaf-binary-numbers/[1022. Sum of Root To Leaf Binary Numbers^]
7185+
|{source_base_url}/_1022_SumOfRootToLeafBinaryNumbers.java[Java]
7186+
|{doc_base_url}/1022-sum-of-root-to-leaf-binary-numbers.adoc[题解]
7187+
|Easy
7188+
|
7189+
71907190
//|{counter:codes}
71917191
//|{leetcode_base_url}/camelcase-matching/[1023. Camelcase Matching^]
71927192
//|{source_base_url}/_1023_CamelcaseMatching.java[Java]
Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,71 @@
11
[#1022-sum-of-root-to-leaf-binary-numbers]
2-
= 1022. Sum of Root To Leaf Binary Numbers
2+
= 1022. 从根到叶的二进制数之和
33

4-
{leetcode}/problems/sum-of-root-to-leaf-binary-numbers/[LeetCode - Sum of Root To Leaf Binary Numbers^]
4+
https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/[LeetCode - 1022. 从根到叶的二进制数之和 ^]
55

6-
Given a binary tree, each node has value `0` or `1`. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is `0 -> 1 -> 1 -> 0 -> 1`, then this could represent `01101` in binary, which is `13`.
6+
给出一棵二叉树,其上每个结点的值都是 `0` `1` 。每一条从根到叶的路径都代表一个从最高有效位开始的二进制数。
77

8-
For all leaves in the tree, consider the numbers represented by the path from the root to that leaf.
8+
* 例如,如果路径为 `0 -> 1 -> 1 -> 0 -> 1`,那么它表示二进制数 `01101`,也就是 `13`
99
10-
Return the sum of these numbers.
10+
对树上的每一片叶子,我们都要找出从根到该叶子的路径所表示的数字。
1111

12-
12+
返回这些数字之和。题目数据保证答案是一个 *32 位* 整数。
1313

14-
*Example 1:*
14+
*示例 1:*
1515

16-
image::https://assets.leetcode.com/uploads/2019/04/04/sum-of-root-to-leaf-binary-numbers.png[{image_attr}]
16+
image::images/1022-01.png[{image_attr}]
1717

18-
[subs="verbatim,quotes,macros"]
19-
----
20-
*Input:* [1,0,1,0,1,0,1]
21-
*Output:* 22
22-
*Explanation:* (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
23-
----
18+
....
19+
输入:root = [1,0,1,0,1,0,1]
20+
输出:22
21+
解释:(100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
22+
....
23+
24+
*示例 2:*
25+
26+
....
27+
输入:root = [0]
28+
输出:0
29+
....
2430

25-
2631

27-
*Note:*
32+
*提示:*
2833

34+
* 树中的节点数在 `[1, 1000]` 范围内
35+
* `Node.val` 仅为 `0``1`
2936
30-
. The number of nodes in the tree is between `1` and `1000`.
31-
. node.val is `0` or `1`.
32-
. The answer will not exceed `2^31 - 1`.
3337
38+
== 思路分析
3439

40+
深度优先遍历,从 `root` 节点开始计算数值,到叶子节点计算完毕加入到总和中即可。
3541

42+
image::images/1022-10.jpeg[{image_attr}]
3643

3744
[[src-1022]]
45+
[tabs]
46+
====
47+
一刷::
48+
+
49+
--
3850
[{java_src_attr}]
3951
----
4052
include::{sourcedir}/_1022_SumOfRootToLeafBinaryNumbers.java[tag=answer]
4153
----
54+
--
55+
56+
// 二刷::
57+
// +
58+
// --
59+
// [{java_src_attr}]
60+
// ----
61+
// include::{sourcedir}/_1022_SumOfRootToLeafBinaryNumbers_2.java[tag=answer]
62+
// ----
63+
// --
64+
====
65+
66+
67+
== 参考资料
4268

69+
. https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/[1022. 从根到叶的二进制数之和 - 官方题解^]
70+
. https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/solutions/1526069/by-ac_oier-1905/[1022. 从根到叶的二进制数之和 - 树的遍历运用题^]
71+
. https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/solutions/1525940/by-nehzil-4gym/[1022. 从根到叶的二进制数之和 - 递归三部曲「图解过程」^]

docs/images/1022-01.png

45.6 KB
Loading

docs/images/1022-10.jpeg

560 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2127,7 +2127,7 @@ include::1020-number-of-enclaves.adoc[leveloffset=+1]
21272127

21282128
// include::1021-remove-outermost-parentheses.adoc[leveloffset=+1]
21292129

2130-
// include::1022-sum-of-root-to-leaf-binary-numbers.adoc[leveloffset=+1]
2130+
include::1022-sum-of-root-to-leaf-binary-numbers.adoc[leveloffset=+1]
21312131

21322132
// include::1023-camelcase-matching.adoc[leveloffset=+1]
21332133

logbook/202503.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,10 @@ endif::[]
828828
|{doc_base_url}/1020-number-of-enclaves.adoc[题解]
829829
|✅ 深度优先遍历。有机会尝试一下广度优先遍历。
830830

831+
|{counter:codes2503}
832+
|{leetcode_base_url}/sum-of-root-to-leaf-binary-numbers/[1022. 从根到叶的二进制数之和^]
833+
|{doc_base_url}/1022-sum-of-root-to-leaf-binary-numbers.adoc[题解]
834+
|✅ 深度优先遍历。
831835

832836
|===
833837

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.TreeNode;
4+
import com.diguage.util.TreeNodes;
5+
6+
import java.util.Arrays;
7+
8+
public class _1022_SumOfRootToLeafBinaryNumbers {
9+
// tag::answer[]
10+
/**
11+
* @author D瓜哥 · https://www.diguage.com
12+
* @since 2025-05-25 23:02:00
13+
*/
14+
int sum = 0;
15+
16+
public int sumRootToLeaf(TreeNode root) {
17+
dfs(root, 0);
18+
return sum;
19+
}
20+
21+
private void dfs(TreeNode root, int head) {
22+
if (root == null) {
23+
return;
24+
}
25+
int num = 2 * head + root.val;
26+
if (root.left == null && root.right == null) {
27+
sum += num;
28+
return;
29+
}
30+
dfs(root.left, num);
31+
dfs(root.right, num);
32+
}
33+
34+
// end::answer[]
35+
public static void main(String[] args) {
36+
new _1022_SumOfRootToLeafBinaryNumbers()
37+
.sumRootToLeaf(TreeNodes.buildTree(Arrays.asList(1, 0, 1, 0, 1, 0, 1)));
38+
}
39+
}

0 commit comments

Comments
 (0)