题目链接
https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/
题目解析
Golang
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
|
func minDepth(root *TreeNode) int { if root == nil { return 0 } leftDepth := minDepth(root.Left) rightDepth := minDepth(root.Right) if root.Left == nil { return 1 + rightDepth }else if root.Right == nil { return 1 + leftDepth }
return 1 + min(leftDepth,rightDepth) }
func min(a,b int)int { if a < b { return a } else { return b } }
|