Movatterモバイル変換


[0]ホーム

URL:


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

Commite4654d4

Browse files
committed
add 111 112 113 solution
1 parent20f87a5 commite4654d4

File tree

10 files changed

+255
-4
lines changed

10 files changed

+255
-4
lines changed

‎generate.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ git reset --hard origin/master
88
git pull
99

1010
# GitBook
11+
#npm install gitbook-cli -g
1112
#gitbook serve --config book.json . gitbook
1213
gitbook build --config book.json. gitbook
1314

@@ -16,4 +17,4 @@ gitbook build --config book.json . gitbook
1617
#npm install --save-dev all-contributors-cli
1718
#npm run contributors:generate
1819
#all-contributors add kylesliu code,blog,design,doc
19-
#all-contributors generate
20+
#all-contributors generate

‎src/0111.Minimum-Depth-of-Binary-Tree/Solution.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ func minDepth(root *TreeNode) int {
1010
ifroot==nil {
1111
return0
1212
}
13-
13+
//左边为空只计算右边
1414
ifroot.Left==nil {
1515
returnminDepth(root.Right)+1
1616
}
17-
17+
//右边为空只计算左边
1818
ifroot.Right==nil {
1919
returnminDepth(root.Left)+1
2020
}
21-
21+
//正常计算左右2个子树
2222
returnmin(minDepth(root.Left),minDepth(root.Right))+1
2323
}
2424

‎src/0112.Path-Sum/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#[112. Path Sum][title]
2+
3+
##Description
4+
5+
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
6+
7+
Note: A leaf is a node with no children.
8+
9+
**Example 1:**
10+
Given the below binary tree and sum = 22,
11+
12+
```
13+
5
14+
/ \
15+
4 8
16+
/ / \
17+
11 13 4
18+
/ \ \
19+
7 2 1
20+
```
21+
22+
23+
**Tags:** Math, String
24+
25+
##题意
26+
>求2数之和
27+
28+
##题解
29+
30+
###思路1
31+
>。。。。
32+
33+
```go
34+
35+
```
36+
37+
###思路2
38+
>思路2
39+
```go
40+
41+
```
42+
43+
##结语
44+
45+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode][me]
46+
47+
[title]:https://leetcode.com/problems/two-sum/description/
48+
[me]:https://github.com/kylesliu/awesome-golang-leetcode

‎src/0112.Path-Sum/Solution.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package Solution
2+
3+
//递归
4+
funchasPathSum(root*TreeNode,sumint)bool {
5+
ifroot==nil {
6+
returnfalse
7+
}
8+
ifroot.Left==nil&&root.Right==nil {
9+
returnroot.Val==sum
10+
}
11+
returnhasPathSum(root.Left,sum-root.Val)||hasPathSum(root.Right,sum-root.Val)
12+
}
13+

‎src/0112.Path-Sum/Solution_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package Solution
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"strconv"
7+
"testing"
8+
)
9+
10+
funcTestSolution(t*testing.T) {
11+
//测试用例
12+
cases:= []struct {
13+
namestring
14+
inputs*TreeNode
15+
sumint
16+
expectbool
17+
}{
18+
{"TestCase",
19+
&TreeNode{Val:5,
20+
Left:&TreeNode{Val:4,Left:nil,Right:nil},
21+
Right:&TreeNode{Val:8,Left:nil,Right:nil},
22+
},
23+
9,
24+
true,
25+
},
26+
}
27+
28+
//开始测试
29+
fori,c:=rangecases {
30+
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
31+
got:=hasPathSum(c.inputs,c.sum)
32+
fmt.Println(got)
33+
if!reflect.DeepEqual(got,c.expect) {
34+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
35+
c.expect,got,c.inputs)
36+
}
37+
})
38+
}
39+
}
40+
41+
//压力测试
42+
funcBenchmarkSolution(b*testing.B) {
43+
44+
}
45+
46+
//使用案列
47+
funcExampleSolution() {
48+
49+
}

‎src/0112.Path-Sum/TreeNode.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package Solution
2+
3+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}

‎src/0113.Path-Sum-II/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#[113. Path Sum II][title]
2+
3+
##Description
4+
5+
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
6+
7+
Note: A leaf is a node with no children.
8+
9+
**Example 1:**
10+
Given the below binary tree and sum = 22,
11+
12+
```
13+
5
14+
/ \
15+
4 8
16+
/ / \
17+
11 13 4
18+
/ \ / \
19+
7 2 5 1
20+
```
21+
22+
```java
23+
[
24+
[5,4,11,2],
25+
[5,8,4,5]
26+
]
27+
```
28+
29+
**Tags:** Math, String
30+
31+
##题意
32+
>求2数之和
33+
34+
##题解
35+
36+
###思路1
37+
>。。。。
38+
39+
```go
40+
41+
```
42+
43+
###思路2
44+
>思路2
45+
```go
46+
47+
```
48+
49+
##结语
50+
51+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode][me]
52+
53+
[title]:https://leetcode.com/problems/two-sum/description/
54+
[me]:https://github.com/kylesliu/awesome-golang-leetcode

‎src/0113.Path-Sum-II/Solution.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package Solution
2+
3+
4+
funcpathSum(root*TreeNode,sumint) [][]int {
5+
varslice [][]int
6+
slice=pathSumHelper(root,sum,slice, []int(nil))
7+
returnslice
8+
}
9+
10+
funcpathSumHelper(n*TreeNode,sumint,slice [][]int,stack []int) [][]int {
11+
ifn==nil {
12+
returnslice
13+
}
14+
sum-=n.Val
15+
stack=append(stack,n.Val)
16+
17+
ifsum==0&&n.Left==nil&&n.Right==nil {
18+
slice=append(slice,append([]int(nil),stack...))
19+
stack=stack[:len(stack)-1]
20+
}
21+
22+
slice=pathSumHelper(n.Left,sum,slice,stack)
23+
slice=pathSumHelper(n.Right,sum,slice,stack)
24+
returnslice
25+
}

‎src/0113.Path-Sum-II/Solution_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"strconv"
6+
"testing"
7+
)
8+
9+
funcTestSolution(t*testing.T) {
10+
//测试用例
11+
cases:= []struct {
12+
namestring
13+
inputs*TreeNode
14+
sumint
15+
expect [][]int
16+
}{
17+
{"TestCase",
18+
&TreeNode{Val:5,
19+
Left:&TreeNode{Val:4,Left:nil,Right:nil},
20+
Right:&TreeNode{Val:8,Left:nil,Right:nil},
21+
},
22+
9,
23+
[][]int{[]int{5,4}},
24+
},
25+
}
26+
27+
//开始测试
28+
fori,c:=rangecases {
29+
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
30+
got:=pathSum(c.inputs,c.sum)
31+
if!reflect.DeepEqual(got,c.expect) {
32+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
33+
c.expect,got,c.inputs)
34+
}
35+
})
36+
}
37+
}
38+
39+
//压力测试
40+
funcBenchmarkSolution(b*testing.B) {
41+
42+
}
43+
44+
//使用案列
45+
funcExampleSolution() {
46+
47+
}

‎src/0113.Path-Sum-II/TreeNode.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package Solution
2+
3+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp