Skip to content

Commit 35e99b5

Browse files
author
luzhipeng
committed
1 parent dde5380 commit 35e99b5

6 files changed

+109
-0
lines changed

README.en.md

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ The data structures mainly includes:
104104
- [0121.best-time-to-buy-and-sell-stock](./problems/121.best-time-to-buy-and-sell-stock.md)
105105
- [0122.best-time-to-buy-and-sell-stock-ii](./problems/122.best-time-to-buy-and-sell-stock-ii.md)
106106
- [0125.valid-palindrome](./problems/125.valid-palindrome.md) 🆕
107+
- [0129.sum-root-to-leaf-numbers](./problems/129.sum-root-to-leaf-numbers.md) 🆕
107108
- [0136.single-number](./problems/136.single-number.md)
108109
- [0155.min-stack](./problems/155.min-stack.md) 🆕
109110
- [0167.two-sum-ii-input-array-is-sorted](./problems/167.two-sum-ii-input-array-is-sorted.md)

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
101101
- [0121.best-time-to-buy-and-sell-stock](./problems/121.best-time-to-buy-and-sell-stock.md)
102102
- [0122.best-time-to-buy-and-sell-stock-ii](./problems/122.best-time-to-buy-and-sell-stock-ii.md)
103103
- [0125.valid-palindrome](./problems/125.valid-palindrome.md) 🆕
104+
- [0129.sum-root-to-leaf-numbers](./problems/129.sum-root-to-leaf-numbers.md) 🆕
104105
- [0136.single-number](./problems/136.single-number.md)
105106
- [0155.min-stack](./problems/155.min-stack.md) 🆕
106107
- [0167.two-sum-ii-input-array-is-sorted](./problems/167.two-sum-ii-input-array-is-sorted.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<mxfile modified="2019-07-05T08:47:06.458Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" etag="O_GMOiCvhsKgaoJgqT56" version="10.8.9" type="device"><diagram id="d_GzZniWwehPkstElGgf" name="第 1 页">7Vzbkps4E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MMHVl8HBhNy+4QQpJsh8Dg4mhvvl9GSIraMEp9mDg4nafRtNkA2R1x8yTkyvDDCOE6RgtpOcIQNF9DVjZBooSONQb8zKMSN9AwpSQX0rALPxB5XHoA1uqFP+DENio4HZuCySElm6mlmDbXarZjixv/xCMNI/NcOZOdTyr8cGS893kKYrR7uxUh0wLm3C1E6OLtZhwPZcraszU3UEsgsnm9oFMcLHm5rREgrkeLp9WO6ppsb0rgju1tSOIUjMLnA7vUtsJEa/VHUY7V3EJZBuGELEJXuA6mi9C7UWSBn0bhLBrrwbJEKGEEilUz9cwI+NmprbWiBltvFACqmcvlWKIad3Hh7SKpdfK8aAmh3TaoZEzOVXi7n9UzMkYgaRr7Bjo41tMtrAcWl7pyY2FuiKdswQtEhGXVJic7SpGSWjGKG6b7JPTWyQpXfldZvYUMgQFM0JFsBaRfWU9VuxUYiKndZYrlTMc0Bs2kpqJoia1rD02VZ774tD1oJoQNZQHKuZMLZ0EF0IoYRLWYbE54KwsYZTgdDaQTgiCDgtajeMQtwwCvHZRiGM3HQvhGOtmkjbyGEEZW1zJwjKJEGd6esBke5AHEvZMLEBEWg8HjJIL1s/xXEsdcM200MiMo9j4wuHAEevNiDHWjhMiDaDlYvOdRy9BhzpuegTaVo/SW50HMcUR8wsARdhHsmGFyfZNRJhkhW4PawX4ve/4otuxVfqxM+f3J+Kf3+vF088W/UY1Dp4GsjO3ZV919bMA6uQEgYhJY0zj7NB2vCS4gjpuyraVEgdfD5IRXP3vcSSB9p9dZLc/Q8=</diagram></mxfile>
Loading
Loading
+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
## 题目地址
2+
3+
https://leetcode.com/problems/sum-root-to-leaf-numbers/description/
4+
5+
## 题目描述
6+
7+
```
8+
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
9+
10+
An example is the root-to-leaf path 1->2->3 which represents the number 123.
11+
12+
Find the total sum of all root-to-leaf numbers.
13+
14+
Note: A leaf is a node with no children.
15+
16+
Example:
17+
18+
Input: [1,2,3]
19+
1
20+
/ \
21+
2 3
22+
Output: 25
23+
Explanation:
24+
The root-to-leaf path 1->2 represents the number 12.
25+
The root-to-leaf path 1->3 represents the number 13.
26+
Therefore, sum = 12 + 13 = 25.
27+
Example 2:
28+
29+
Input: [4,9,0,5,1]
30+
4
31+
/ \
32+
9 0
33+
/ \
34+
5 1
35+
Output: 1026
36+
Explanation:
37+
The root-to-leaf path 4->9->5 represents the number 495.
38+
The root-to-leaf path 4->9->1 represents the number 491.
39+
The root-to-leaf path 4->0 represents the number 40.
40+
Therefore, sum = 495 + 491 + 40 = 1026.
41+
42+
```
43+
44+
## 思路
45+
46+
这是一道非常适合训练递归的题目。虽然题目不难,但是要想一次写正确,并且代码要足够优雅却不是很容易。
47+
48+
这里我们的思路是定一个递归的helper函数,用来帮助我们完成递归操作。
49+
递归函数的功能是将它的左右子树相加,注意这里不包括这个节点本身,否则会多加,
50+
我们其实关注的就是叶子节点的值,然后通过层层回溯到root,返回即可。
51+
52+
整个过程如图所示:
53+
54+
![129.sum-root-to-leaf-numbers-1](../assets/problems/129.sum-root-to-leaf-numbers-1.jpg)
55+
56+
57+
那么数字具体的计算逻辑,如图所示,相信大家通过这个不难发现规律:
58+
59+
![129.sum-root-to-leaf-numbers-2](../assets/problems/129.sum-root-to-leaf-numbers-2.jpg)
60+
61+
## 关键点解析
62+
63+
- 递归分析
64+
65+
## 代码
66+
67+
```js
68+
/*
69+
* @lc app=leetcode id=129 lang=javascript
70+
*
71+
* [129] Sum Root to Leaf Numbers
72+
*/
73+
function helper(node, cur) {
74+
if (node === null) return 0;
75+
const next = node.val + cur * 10;
76+
77+
if (node.left === null && node.right === null) return next;
78+
79+
const l = helper(node.left, next);
80+
const r = helper(node.right, next);
81+
82+
return l + r;
83+
}
84+
/**
85+
* Definition for a binary tree node.
86+
* function TreeNode(val) {
87+
* this.val = val;
88+
* this.left = this.right = null;
89+
* }
90+
*/
91+
/**
92+
* @param {TreeNode} root
93+
* @return {number}
94+
*/
95+
var sumNumbers = function(root) {
96+
// tag: `tree` `dfs` `math`
97+
return helper(root, 0);
98+
};
99+
```
100+
101+
## 相关题目
102+
103+
- [sum-of-root-to-leaf-binary-numbers](https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/)
104+
105+
> 这道题和本题太像了,跟一道题没啥区别
106+

0 commit comments

Comments
 (0)