Skip to content

Commit c67fd09

Browse files
committed
Update 129.求根到叶子节点数字之和 with cpp code
1 parent 7fec83a commit c67fd09

File tree

1 file changed

+86
-8
lines changed

1 file changed

+86
-8
lines changed

basic/binary-tree/15.sum-root-to-leaf-numbers.md

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ https://leetcode-cn.com/problems/sum-root-to-leaf-numbers
77
- [方法 1:递归](#方法-1递归)
88
- [思路](#思路)
99
- [复杂度](#复杂度)
10-
- [代码](#代码)
10+
- [代码(JavaScript/Python/C++)](#代码javascriptpythonc)
1111
- [方法 2:BFS](#方法-2bfs)
1212
- [思路](#思路-1)
1313
- [复杂度](#复杂度-1)
14-
- [代码](#代码-1)
14+
- [代码(JavaScript/C++)](#代码javascriptc)
1515

1616
## 题目描述
1717

@@ -58,11 +58,11 @@ https://leetcode-cn.com/problems/sum-root-to-leaf-numbers
5858

5959
### 思路
6060

61-
lucifer 的[递归小技巧](https://github.com/leetcode-pp/91alg-1/issues/32#issuecomment-643620727)练习课堂。
61+
lucifer 的**递归小技巧**练习课堂。
6262

6363
1. 定义函数功能,先不用管其具体实现。
6464

65-
我们需要一个函数,给定一个二叉树的根节点,返回根节点到各个叶子节点连成的数字的和。假设我们已经有这个函数 `F`那问题就转化为 `F(root)`
65+
首先我们需要一个函数,给定一个二叉树的根节点,返回根节点到各个叶子节点连成的数字的和。假设我们已经有这个函数 `F`那问题转化成 `F(root)`
6666

6767
唔...其实问题还要复杂一丢丢,因为我们得要遍历到叶子节点的时候才能确定连成的数字,而要知道这个数字是什么,还要知道这个叶子节点的父节点,以及它父节点的父节点,...,一直到根节点,也就是说我们需要在遍历开始的时候就用一个变量来记录走过的节点。那现在我们的问题就转化成了 `F(root, num)`
6868

@@ -83,7 +83,7 @@ lucifer 的[递归小技巧](https://github.com/leetcode-pp/91alg-1/issues/32#is
8383
- 时间复杂度: $O(N)$,N 为二叉树节点数。
8484
- 空间复杂度: $O(h)$,h 为二叉树高度。
8585

86-
### 代码
86+
### 代码(JavaScript/Python/C++)
8787

8888
JavaScript Code
8989

@@ -129,19 +129,48 @@ class Solution(object):
129129
return self.sumNumbers(root.left, num) + self.sumNumbers(root.right, num)
130130
```
131131

132+
C++ Code
133+
134+
```cpp
135+
/**
136+
* Definition for a binary tree node.
137+
* struct TreeNode {
138+
* int val;
139+
* TreeNode *left;
140+
* TreeNode *right;
141+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
142+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
143+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
144+
* };
145+
*/
146+
class Solution {
147+
public:
148+
int sumNumbers(TreeNode* root) {
149+
return sumNumbers(root, 0);
150+
}
151+
int sumNumbers(TreeNode* root, int num) {
152+
if (root == nullptr) return 0;
153+
num = num * 10 + root->val;
154+
if (root->left == nullptr && root->right == nullptr) return num;
155+
return sumNumbers(root->left, num) + sumNumbers(root->right, num);
156+
}
157+
158+
};
159+
```
160+
132161
## 方法 2:BFS
133162
134163
### 思路
135164
136-
- 层级遍历,将每一层节点的数字传给下一层,存在 `node.val` 中。
137-
- 如果当前节点没有子节点了,就把它的值加到结果中
165+
- 对二叉树进行层级遍历,将每一层节点的数字传给下一层,存在 `node.val` 中。
166+
- 如果当前节点没有子节点了,就可以把它的值加到结果中去了
138167
139168
### 复杂度
140169
141170
- 时间复杂度: $O(N)$,N 为二叉树节点数。
142171
- 空间复杂度: $O(q)$,q 为队列长度。最坏情况是满二叉树,此时 q 为 $2/N$,即为 $O(N)$,N 为二叉树节点数。
143172
144-
## 代码
173+
## 代码(JavaScript/C++)
145174
146175
JavaScript Code
147176
@@ -184,3 +213,52 @@ var sumNumbers = function (root) {
184213
return sum;
185214
};
186215
```
216+
217+
C++ Code
218+
219+
```cpp
220+
/**
221+
* Definition for a binary tree node.
222+
* struct TreeNode {
223+
* int val;
224+
* TreeNode *left;
225+
* TreeNode *right;
226+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
227+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
228+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
229+
* };
230+
*/
231+
class Solution {
232+
public:
233+
int sumNumbers(TreeNode* root) {
234+
if (!root) return 0;
235+
236+
int total = 0;
237+
queue<TreeNode*> level;
238+
level.push(root);
239+
240+
while (!level.empty()) {
241+
int size = level.size();
242+
while (size--) {
243+
TreeNode* node = level.front();
244+
level.pop();
245+
246+
if (node->left) {
247+
node->left->val += node->val * 10;
248+
level.push(node->left);
249+
}
250+
if (node->right) {
251+
node->right->val += node->val * 10;
252+
level.push(node->right);
253+
}
254+
if (!node->left && !node->right) {
255+
total += node->val;
256+
}
257+
}
258+
}
259+
return total;
260+
}
261+
};
262+
```
263+
264+
更多题解可以访问:[https://github.com/suukii/91-days-algorithm](https://github.com/suukii/91-days-algorithm)

0 commit comments

Comments
 (0)