Skip to content

Commit 6c05203

Browse files
committed
Add solution 1022
1 parent ccde9a0 commit 6c05203

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

website/content/ChapterFour/1000~1099/1021.Remove-Outermost-Parentheses.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,5 @@ func removeOuterParentheses1(S string) string {
126126
----------------------------------------------
127127
<div style="display: flex;justify-content: space-between;align-items: center;">
128128
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1020.Number-of-Enclaves/">⬅️上一页</a></p>
129-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1025.Divisor-Game/">下一页➡️</a></p>
129+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1022.Sum-of-Root-To-Leaf-Binary-Numbers/">下一页➡️</a></p>
130130
</div>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# [1022. Sum of Root To Leaf Binary Numbers](https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/)
2+
3+
## 题目
4+
5+
You are given the root of a binary tree where each node has a value 0 or 1. Each root-to-leaf path represents a binary number starting with the most significant bit.
6+
7+
For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13.
8+
For all leaves in the tree, consider the numbers represented by the path from the root to that leaf. Return the sum of these numbers.
9+
10+
The test cases are generated so that the answer fits in a 32-bits integer.
11+
12+
**Example 1:**
13+
14+
```c
15+
Input: root = [1,0,1,0,1,0,1]
16+
Output: 22
17+
Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
18+
```
19+
20+
**Example 2:**
21+
22+
```c
23+
Input: root = [0]
24+
Output: 0
25+
```
26+
27+
**Constraints:**
28+
29+
- The number of nodes in the tree is in the range `[1, 1000]`.
30+
31+
- `Node.val` is `0` or `1`.
32+
33+
34+
## 题目大意
35+
36+
给定一棵结点值都是`0``1`的二叉树,每条从根结点到叶结点的路径都代表一个从最高有效位开始的二进制数。
37+
38+
返回从根节点到所有叶结点的路径所表示的数字之和。
39+
40+
41+
## 解题思路
42+
43+
采用递归的方式对根结点`root`进行后序遍历(左子树-右子树-根结点)。
44+
45+
**递归函数的返回值**
46+
47+
递归遍历每个结点时,计算从根结点到当前访问结点的所表示数值`sum`都用到了上次的计算结果,所以递归函数的返回值是当前访问结点的计算结果值。
48+
49+
**递归函数的逻辑**
50+
51+
- 当前遍历结点为`nil`,表示本层递归结束了,直接`return 0`
52+
53+
- 如果当前访问结点是叶结点,则返回从根结点到该结点所表示的数值`sum`
54+
- 如果当前访问结点不是叶结点,则返回左子树和右子树所对应的结果之和。
55+
56+
57+
----------------------------------------------
58+
<div style="display: flex;justify-content: space-between;align-items: center;">
59+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1021.Remove-Outermost-Parentheses/">⬅️上一页</a></p>
60+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1025.Divisor-Game/">下一页➡️</a></p>
61+
</div>

website/content/ChapterFour/1000~1099/1025.Divisor-Game.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ func divisorGame(N int) bool {
6464

6565
----------------------------------------------
6666
<div style="display: flex;justify-content: space-between;align-items: center;">
67-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1021.Remove-Outermost-Parentheses/">⬅️上一页</a></p>
67+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1022.Sum-of-Root-To-Leaf-Binary-Numbers/">⬅️上一页</a></p>
6868
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1026.Maximum-Difference-Between-Node-and-Ancestor/">下一页➡️</a></p>
6969
</div>

0 commit comments

Comments
 (0)