Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7891d79

Browse files
add LeetCode 437. 路径总和 III
1 parent e474f16 commit 7891d79

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4uanNkZWxpdnIubmV0L2doL2Nob2NvbGF0ZTE5OTkvY2RuL2ltZy8yMDIwMDgyODE0NTUyMS5qcGc?x-oss-process=image/format,png)
2+
>仰望星空的人,不应该被嘲笑
3+
4+
## 题目描述
5+
给定一个二叉树,它的每个结点都存放着一个整数值。
6+
7+
找出路径和等于给定数值的路径总数。
8+
9+
路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点)。
10+
11+
二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数。
12+
13+
示例:
14+
15+
```javascript
16+
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
17+
18+
10
19+
/ \
20+
5 -3
21+
/ \ \
22+
3 2 11
23+
/ \ \
24+
3 -2 1
25+
26+
返回 3。和等于 8 的路径有:
27+
28+
1. 5 -> 3
29+
2. 5 -> 2 -> 1
30+
3. -3 -> 11
31+
```
32+
33+
来源:力扣(LeetCode)
34+
链接:https://leetcode-cn.com/problems/path-sum-iii
35+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
36+
37+
38+
39+
40+
## 解题思路
41+
42+
本题采用方式就是先序遍历,对于遍历到的每个节点,我们都进行一次 `dfs`,但是考虑本题的数字范围为负数,对于当前一条路我们得到了一条路径后,假如后面还有路可以走,那么我们还是继续走,因为后面可能出现正负抵消的情况。
43+
44+
面试中如果遇到题例没有明确说明数字范围,建议和面试官沟通。
45+
46+
```javascript
47+
/**
48+
* Definition for a binary tree node.
49+
* function TreeNode(val, left, right) {
50+
* this.val = (val===undefined ? 0 : val)
51+
* this.left = (left===undefined ? null : left)
52+
* this.right = (right===undefined ? null : right)
53+
* }
54+
*/
55+
/**
56+
* @param {TreeNode} root
57+
* @param {number} sum
58+
* @return {number}
59+
*/
60+
var pathSum = function (root, sum) {
61+
// 定义一个计时器
62+
let cnt = 0;
63+
// 先序遍历所有根节点
64+
let preOrder = (root, sum) => {
65+
if (root == null) return;
66+
dfs(root, sum);
67+
preOrder(root.left, sum);
68+
preOrder(root.right, sum);
69+
}
70+
let dfs = (root, sum) => {
71+
if (root == null) return;
72+
sum -= root.val;
73+
// 求和满足,累加
74+
if (sum === 0) cnt++;
75+
// 递归左右子树,如果当前和为0了,但是下面还是有路,还是继续走下去
76+
// 因为本题数值范围存在负数,可能继续走下去还存在满足条件的路径
77+
dfs(root.left, sum);
78+
dfs(root.right, sum);
79+
}
80+
preOrder(root, sum);
81+
return cnt;
82+
};
83+
```
84+
85+
86+
## 最后
87+
文章产出不易,还望各位小伙伴们支持一波!
88+
89+
往期精选:
90+
91+
<a href="https://github.com/Chocolate1999/Front-end-learning-to-organize-notes">小狮子前端の笔记仓库</a>
92+
93+
<a href="https://github.com/Chocolate1999/leetcode-javascript">leetcode-javascript:LeetCode 力扣的 JavaScript 解题仓库,前端刷题路线(思维导图)</a>
94+
95+
小伙伴们可以在Issues中提交自己的解题代码,🤝 欢迎Contributing,可打卡刷题,Give a ⭐️ if this project helped you!
96+
97+
98+
<a href="https://yangchaoyi.vip/">访问超逸の博客</a>,方便小伙伴阅读玩耍~
99+
100+
![](https://img-blog.csdnimg.cn/2020090211491121.png#pic_center)
101+
102+
```javascript
103+
学如逆水行舟,不进则退
104+
```
105+
106+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /