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
| // @Title: 二叉树的最小深度 (Minimum Depth of Binary Tree)
// @Author: 15816537946@163.com
// @Date: 2019-11-24 21:11:58
// @Runtime: 8 ms
// @Memory: 5.1 MB
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func minDepth(node *TreeNode) int {
if node == nil {
return 0
}
if node.Left == nil {
return 1+ minDepth(node.Right)
}
if node.Right == nil {
return 1 + minDepth(node.Left)
}
return 1 + min(minDepth(node.Left), minDepth(node.Right))
}
func min(a, b int) int {
if a > b {
return b
}
return a
}
|