题目:
题解:
func minDepth(root *TreeNode) int {
if root == nil {
return 0
}
queue := []*TreeNode{}
count := []int{}
queue = append(queue, root)
count = append(count, 1)
for i := 0; i < len(queue); i++ {
node := queue[i]
depth := count[i]
if node.Left == nil && node.Right == nil {
return depth
}
if node.Left != nil {
queue = append(queue, node.Left)
count = append(count, depth + 1)
}
if node.Right != nil {
queue = append(queue, node.Right)
count = append(count, depth + 1)
}
}
return 0
}