• 104.二叉树的最大深度
  • 559.n叉树的最大深度
    • 递归
    • 广度优先
    • 深度优先
  • 111.二叉树的最小深度
    • 递归(有坑)
    • 广度优先

二叉树的最小深度 - 递归

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func minDepth(root *TreeNode) int {
	if root == nil {
		// 空节点
		return 0
	}
	if root.Left == nil && root.Right == nil {
		// 叶子节点
		return 1
	}
 
	lDepth, rDepth := math.MaxInt, math.MaxInt
	if root.Left != nil {
		lDepth = minDepth(root.Left)
	}
	if root.Right != nil {
		rDepth = minDepth(root.Right)
	}
 
	return min(lDepth, rDepth) + 1
}