- Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsolution.go
51 lines (46 loc) · 852 Bytes
/
solution.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
package leetcode
/*
* @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
* }
*/
typeTreeNodestruct {
Valint
Left*TreeNode
Right*TreeNode
}
funcminDepth(root*TreeNode) int {
ifroot==nil {
return0
}
count:=1
queue:= []*TreeNode{root}
forlen(queue) >0 {
l:=len(queue)
fori:=0; i<l; i++ {
node:=queue[i]
ifnode.Left==nil&&node.Right==nil {
returncount
}
ifnode.Left!=nil {
queue=append(queue, node.Left)
}
ifnode.Right!=nil {
queue=append(queue, node.Right)
}
}
count++
queue=queue[l:]
}
returncount
}
// @lc code=end