Skip to content

Commit 99fe55c

Browse files
committed
Update 0437
1 parent 65f4c4f commit 99fe55c

26 files changed

+687
-564
lines changed

README.md

Lines changed: 389 additions & 385 deletions
Large diffs are not rendered by default.

leetcode/0437.Path-Sum-III/437. Path Sum III.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,30 @@ type TreeNode = structures.TreeNode
1616
* }
1717
*/
1818

19+
// 解法一 带缓存 dfs
20+
func pathSum(root *TreeNode, targetSum int) int {
21+
prefixSum := make(map[int]int)
22+
prefixSum[0] = 1
23+
return dfs(root, prefixSum, 0, targetSum)
24+
}
25+
26+
func dfs(root *TreeNode, prefixSum map[int]int, cur, sum int) int {
27+
if root == nil {
28+
return 0
29+
}
30+
cur += root.Val
31+
cnt := 0
32+
if v, ok := prefixSum[cur-sum]; ok {
33+
cnt = v
34+
}
35+
prefixSum[cur]++
36+
cnt += dfs(root.Left, prefixSum, cur, sum)
37+
cnt += dfs(root.Right, prefixSum, cur, sum)
38+
prefixSum[cur]--
39+
return cnt
40+
}
41+
42+
// 解法二
1943
func pathSumIII(root *TreeNode, sum int) int {
2044
if root == nil {
2145
return 0
@@ -39,25 +63,3 @@ func findPath437(root *TreeNode, sum int) int {
3963
res += findPath437(root.Right, sum-root.Val)
4064
return res
4165
}
42-
43-
func pathSum(root *TreeNode, targetSum int) int {
44-
prefixSum := make(map[int]int)
45-
prefixSum[0] = 1
46-
return dfs(root, prefixSum, 0, targetSum)
47-
}
48-
49-
func dfs(root *TreeNode, prefixSum map[int]int, cur, sum int) int {
50-
if root == nil {
51-
return 0
52-
}
53-
cur += root.Val
54-
cnt := 0
55-
if v, ok := prefixSum[cur-sum]; ok {
56-
cnt = v
57-
}
58-
prefixSum[cur]++
59-
cnt += dfs(root.Left, prefixSum, cur, sum)
60-
cnt += dfs(root.Right, prefixSum, cur, sum)
61-
prefixSum[cur]--
62-
return cnt
63-
}

leetcode/0437.Path-Sum-III/README.md

100755100644
Lines changed: 95 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,34 @@
33

44
## 题目
55

6-
You are given a binary tree in which each node contains an integer value.
6+
Given the `root` of a binary tree and an integer `targetSum`, return *the number of paths where the sum of the values along the path equals* `targetSum`.
77

8-
Find the number of paths that sum to a given value.
8+
The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).
99

10-
The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).
10+
**Example 1:**
1111

12-
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
12+
![https://assets.leetcode.com/uploads/2021/04/09/pathsum3-1-tree.jpg](https://assets.leetcode.com/uploads/2021/04/09/pathsum3-1-tree.jpg)
1313

14-
**Example:**
14+
```
15+
Input: root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
16+
Output: 3
17+
Explanation: The paths that sum to 8 are shown.
1518
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-
Return 3. The paths that sum to 8 are:
27-
28-
1. 5 -> 3
29-
2. 5 -> 2 -> 1
30-
3. -3 -> 11
19+
```
3120

21+
**Example 2:**
22+
23+
```
24+
Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
25+
Output: 3
26+
27+
```
28+
29+
**Constraints:**
30+
31+
- The number of nodes in the tree is in the range `[0, 1000]`.
32+
- `109 <= Node.val <= 109`
33+
- `1000 <= targetSum <= 1000`
3234

3335
## 题目大意
3436

@@ -42,3 +44,76 @@ The tree has no more than 1,000 nodes and the values are in the range -1,000,000
4244
- 注意这一题可能出现负数的情况,节点和为 sum,并不一定是最终情况,有可能下面还有正数节点和负数节点相加正好为 0,那么这也是一种情况。一定要遍历到底。
4345
- 一个点是否为 sum 的起点,有 3 种情况,第一种情况路径包含该 root 节点,如果包含该结点,就在它的左子树和右子树中寻找和为 `sum-root.Val` 的情况。第二种情况路径不包含该 root 节点,那么就需要在它的左子树和右子树中分别寻找和为 sum 的结点。
4446

47+
48+
49+
## 代码
50+
51+
```go
52+
53+
package leetcode
54+
55+
import (
56+
"github.com/halfrost/LeetCode-Go/structures"
57+
)
58+
59+
// TreeNode define
60+
type TreeNode = structures.TreeNode
61+
62+
/**
63+
* Definition for a binary tree node.
64+
* type TreeNode struct {
65+
* Val int
66+
* Left *TreeNode
67+
* Right *TreeNode
68+
* }
69+
*/
70+
71+
// 解法一 带缓存 dfs
72+
func pathSum(root *TreeNode, targetSum int) int {
73+
prefixSum := make(map[int]int)
74+
prefixSum[0] = 1
75+
return dfs(root, prefixSum, 0, targetSum)
76+
}
77+
78+
func dfs(root *TreeNode, prefixSum map[int]int, cur, sum int) int {
79+
if root == nil {
80+
return 0
81+
}
82+
cur += root.Val
83+
cnt := 0
84+
if v, ok := prefixSum[cur-sum]; ok {
85+
cnt = v
86+
}
87+
prefixSum[cur]++
88+
cnt += dfs(root.Left, prefixSum, cur, sum)
89+
cnt += dfs(root.Right, prefixSum, cur, sum)
90+
prefixSum[cur]--
91+
return cnt
92+
}
93+
94+
// 解法二
95+
func pathSumIII(root *TreeNode, sum int) int {
96+
if root == nil {
97+
return 0
98+
}
99+
res := findPath437(root, sum)
100+
res += pathSumIII(root.Left, sum)
101+
res += pathSumIII(root.Right, sum)
102+
return res
103+
}
104+
105+
// 寻找包含 root 这个结点,且和为 sum 的路径
106+
func findPath437(root *TreeNode, sum int) int {
107+
if root == nil {
108+
return 0
109+
}
110+
res := 0
111+
if root.Val == sum {
112+
res++
113+
}
114+
res += findPath437(root.Left, sum-root.Val)
115+
res += findPath437(root.Right, sum-root.Val)
116+
return res
117+
}
118+
119+
```

website/config.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,17 @@ disablePathToLower = true
9898
# Used for 'Last Modified' and 'Edit this page' links.
9999
BookRepo = 'https://github.com/halfrost/LeetCode-Go'
100100

101+
# Specifies commit portion of the link to the page's last modified commit hash for 'doc' page
102+
# type.
103+
# Required if 'BookRepo' param is set.
104+
# Value used to construct a URL consisting of BookRepo/BookCommitPath/<commit-hash>
105+
# Github uses 'commit', Bitbucket uses 'commits'
106+
BookCommitPath = 'commit'
107+
101108
# Enable "Edit this page" links for 'doc' page type.
102109
# Disabled by default. Uncomment to enable. Requires 'BookRepo' param.
103110
# Path must point to 'content' directory of repo.
104-
BookEditPath = 'tree/master/website/content'
111+
BookEditPath = 'tree/master/website/'
105112

106113
# Configure the date format used on the pages
107114
# - In git information

website/content/ChapterFour/0400~0499/0437.Path-Sum-III.md

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,34 @@
33

44
## 题目
55

6-
You are given a binary tree in which each node contains an integer value.
6+
Given the `root` of a binary tree and an integer `targetSum`, return *the number of paths where the sum of the values along the path equals* `targetSum`.
77

8-
Find the number of paths that sum to a given value.
8+
The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).
99

10-
The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).
10+
**Example 1:**
1111

12-
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
12+
![https://assets.leetcode.com/uploads/2021/04/09/pathsum3-1-tree.jpg](https://assets.leetcode.com/uploads/2021/04/09/pathsum3-1-tree.jpg)
1313

14-
**Example**:
14+
```
15+
Input: root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
16+
Output: 3
17+
Explanation: The paths that sum to 8 are shown.
18+
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
25+
Output: 3
26+
27+
```
1528

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-
Return 3. The paths that sum to 8 are:
27-
28-
1. 5 -> 3
29-
2. 5 -> 2 -> 1
30-
3. -3 -> 11
29+
**Constraints:**
3130

31+
- The number of nodes in the tree is in the range `[0, 1000]`.
32+
- `109 <= Node.val <= 109`
33+
- `1000 <= targetSum <= 1000`
3234

3335
## 题目大意
3436

@@ -50,6 +52,13 @@ The tree has no more than 1,000 nodes and the values are in the range -1,000,000
5052

5153
package leetcode
5254

55+
import (
56+
"github.com/halfrost/LeetCode-Go/structures"
57+
)
58+
59+
// TreeNode define
60+
type TreeNode = structures.TreeNode
61+
5362
/**
5463
* Definition for a binary tree node.
5564
* type TreeNode struct {
@@ -58,6 +67,31 @@ package leetcode
5867
* Right *TreeNode
5968
* }
6069
*/
70+
71+
// 解法一 带缓存 dfs
72+
func pathSum(root *TreeNode, targetSum int) int {
73+
prefixSum := make(map[int]int)
74+
prefixSum[0] = 1
75+
return dfs(root, prefixSum, 0, targetSum)
76+
}
77+
78+
func dfs(root *TreeNode, prefixSum map[int]int, cur, sum int) int {
79+
if root == nil {
80+
return 0
81+
}
82+
cur += root.Val
83+
cnt := 0
84+
if v, ok := prefixSum[cur-sum]; ok {
85+
cnt = v
86+
}
87+
prefixSum[cur]++
88+
cnt += dfs(root.Left, prefixSum, cur, sum)
89+
cnt += dfs(root.Right, prefixSum, cur, sum)
90+
prefixSum[cur]--
91+
return cnt
92+
}
93+
94+
// 解法二
6195
func pathSumIII(root *TreeNode, sum int) int {
6296
if root == nil {
6397
return 0
@@ -82,6 +116,7 @@ func findPath437(root *TreeNode, sum int) int {
82116
return res
83117
}
84118

119+
85120
```
86121

87122

0 commit comments

Comments
 (0)