File tree Expand file tree Collapse file tree 3 files changed +125
-0
lines changed
src/0111.Minimum-Depth-of-Binary-Tree Expand file tree Collapse file tree 3 files changed +125
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 1+ package Solution
2+
3+ type TreeNode struct {
4+ Val int
5+ Left * TreeNode
6+ Right * TreeNode
7+ }
8+
9+ func minDepth (root * TreeNode ) int {
10+ if root == nil {
11+ return 0
12+ }
13+
14+ if root .Left == nil {
15+ return minDepth (root .Right ) + 1
16+ }
17+
18+ if root .Right == nil {
19+ return minDepth (root .Left ) + 1
20+ }
21+
22+ return min (minDepth (root .Left ), minDepth (root .Right )) + 1
23+ }
24+
25+ func min (x , y int ) int {
26+ if x < y {
27+ return x
28+ }
29+ return y
30+ }
Original file line number Diff line number Diff line change 1+ package Solution
2+
3+ import (
4+ "reflect"
5+ "strconv"
6+ "testing"
7+ )
8+
9+ func TestSolution (t * testing.T ) {
10+ // 测试用例
11+ cases := []struct {
12+ name string
13+ inputs * TreeNode
14+ expect int
15+ }{
16+ {"TestCase" ,
17+ & TreeNode {Val : 3 ,
18+ Left : & TreeNode {Val : 9 , Left : nil , Right : nil },
19+ Right : & TreeNode {Val : 20 ,
20+ Left : & TreeNode {Val : 15 , Left : nil , Right : nil },
21+ Right : & TreeNode {Val : 7 , Left : nil , Right : nil },
22+ },
23+ },
24+ 2 },
25+ }
26+
27+ // 开始测试
28+ for i , c := range cases {
29+ t .Run (c .name + " " + strconv .Itoa (i ), func (t * testing.T ) {
30+ got := minDepth (c .inputs )
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+ func BenchmarkSolution (b * testing.B ) {
41+
42+ }
43+
44+ // 使用案列
45+ func ExampleSolution () {
46+
47+ }
You can’t perform that action at this time.
0 commit comments