-
Notifications
You must be signed in to change notification settings - Fork 0
/
111.minimum-depth-of-binary-tree.go
138 lines (126 loc) · 2.28 KB
/
111.minimum-depth-of-binary-tree.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* @lc app=leetcode id=111 lang=golang
*
* [111] Minimum Depth of Binary Tree
*
* https://leetcode.com/problems/minimum-depth-of-binary-tree/description/
*
* algorithms
* Easy (36.83%)
* Likes: 1216
* Dislikes: 646
* Total Accepted: 393K
* Total Submissions: 1.1M
* Testcase Example: '[3,9,20,null,null,15,7]'
*
* Given a binary tree, find its minimum depth.
*
* The minimum depth is the number of nodes along the shortest path from the
* root node down to the nearest leaf node.
*
* Note: A leaf is a node with no children.
*
* Example:
*
* Given binary tree [3,9,20,null,null,15,7],
*
*
* 3
* / \
* 9 20
* / \
* 15 7
*
* return its minimum depth = 2.
*
*/
// @lc code=start
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func minDepth(root *TreeNode) int {
//bfs
//return bfs(root)
//recursive
//return recursive(root)
//dfs
return dfsSolution(root)
}
//bfs solution
func bfs(root *TreeNode) int {
if root == nil {
return 0
}
var mdepth int
queue := make([]*TreeNode, 0)
mdepth = 1
queue = append(queue, root)
re:
for len(queue) != 0 {
tempQueue := make([]*TreeNode, 0)
for _, node := range queue {
if node.Left == nil && node.Right == nil {
break re
}
if node.Left != nil {
tempQueue = append(tempQueue, node.Left)
}
if node.Right != nil {
tempQueue = append(tempQueue, node.Right)
}
}
mdepth++
queue = tempQueue
}
return mdepth
}
//recursive solution
func recursive(root *TreeNode) int {
if root == nil {
return 0
}
if root.Left == nil && root.Right == nil {
return 1
}
m1 := recursive(root.Left)
m2 := recursive(root.Right)
if root.Left == nil || root.Right == nil {
return m1 + m2 + 1
}
return intMin(m1, m2) + 1
}
func intMin(m1, m2 int) int {
if m1 > m2 {
m1 = m2
}
return m1
}
var min int
//dfs solution
func dfsSolution(root *TreeNode) int {
if root == nil {
return 0
}
min = 1<<32 - 1
dfs(root, 1)
return min
}
func dfs(root *TreeNode, depth int) {
if root.Left == nil && root.Right == nil {
if depth < min {
min = depth
}
}
if root.Left != nil {
dfs(root.Left, depth+1)
}
if root.Right != nil {
dfs(root.Right, depth+1)
}
}
// @lc code=end