Skip to content

Commit 77d0d28

Browse files
aQuaaQua
aQua
authored and
aQua
committed
437 added
1 parent 3fa0716 commit 77d0d28

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# [437. Path Sum III](https://leetcode.com/problems/path-sum-iii/)
2+
3+
## 题目
4+
You are given a binary tree in which each node contains an integer value.
5+
6+
Find the number of paths that sum to a given value.
7+
8+
The path does not need to start or end at the root or a leaf, but it must go downwards
9+
(traveling only from parent nodes to child nodes).
10+
11+
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
12+
13+
Example:
14+
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
15+
```
16+
10
17+
/ \
18+
5 -3
19+
/ \ \
20+
3 2 11
21+
/ \ \
22+
3 -2 1
23+
```
24+
Return 3. The paths that sum to 8 are:
25+
1. 5 -> 3
26+
2. 5 -> 2 -> 1
27+
3. -3 -> 11
28+
29+
## 解题思路
30+
31+
见程序注释
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package Problem0437
2+
3+
import (
4+
"github.com/aQuaYi/LeetCode-in-Golang/kit"
5+
)
6+
type TreeNode = kit.TreeNode
7+
8+
func pathSum(root *TreeNode, sum int) int {
9+
res :=0
10+
11+
return res
12+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package Problem0437
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/aQuaYi/LeetCode-in-Golang/kit"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func Test_Problem0437(t *testing.T) {
13+
ast := assert.New(t)
14+
15+
// tcs is testcase slice
16+
tcs := []struct {
17+
pre, in []int
18+
sum int
19+
ans int
20+
}{
21+
22+
{
23+
[]int{10, 5, 3, 3, -2, 2, 1, -3, 11},
24+
[]int{3, 3, -2, 5, 2, 1, 10, -3, 11},
25+
8,
26+
3,
27+
},
28+
29+
// 可以多个 testcase
30+
}
31+
32+
for _, tc := range tcs {
33+
fmt.Printf("~~%v~~\n", tc)
34+
35+
ast.Equal(tc.ans, pathSum(kit.PreIn2Tree(tc.pre, tc.in), tc.sum), "输入:%v", tc)
36+
}
37+
}

0 commit comments

Comments
 (0)