-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path111.二叉树的最小深度.go
92 lines (83 loc) · 1.51 KB
/
111.二叉树的最小深度.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* @lc app=leetcode.cn id=111 lang=golang
*
* [111] 二叉树的最小深度
*/
// @lc code=start
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
package main
import "fmt"
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func minDepth(root *TreeNode) int {
if root == nil {
return 0
}
if root.Left != nil && root.Right != nil {
return min(minDepth(root.Left), minDepth(root.Right)) + 1
}
if root.Left != nil {
return minDepth(root.Left) + 1
} else {
return minDepth(root.Right) + 1
}
}
// @lc code=end
func min(a, b int) int {
if a < b {
return a
} else {
return b
}
}
func print(root *TreeNode){
if root == nil {
return
}
stack := []*TreeNode{}
stack = append(stack, root)
for len(stack) > 0 {
size := len(stack)
for i := 0 ; i < size ; i++ {
tmp := stack[0]
stack = stack[1:]
fmt.Printf("%d ", tmp.Val)
if tmp.Left != nil {
stack = append(stack, tmp.Left)
}
if tmp.Right != nil {
stack = append(stack, tmp.Right)
}
}
fmt.Println()
}
}
// @lc code=end
func main(){
t11 := TreeNode{Val: 5}
t21 := TreeNode{Val: 1}
// t22 := TreeNode{Val: 7}
t33 := TreeNode{Val: 6}
// t34 := TreeNode{Val: 9}
t44 := TreeNode{Val: 9}
// t11.Left = &t21
// t11.Right = &t22
// t22.Left = &t33
// t22.Right = &t34
// t34.Right = &t44
t11.Right = &t21
t21.Right = &t33
t33.Right = &t44
print(&t11)
fmt.Printf("min height is %d\n", minDepth(&t11))
}