题目描述:

求给定二叉树的最小深度。最小深度是指树的根结点到最近叶子结点的最短路径上结点的数量。

解题思路:

递归

与求最大深度稍有不同,需要考虑左子树为空或右子树为空的情况。

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int run(TreeNode root) {
        if(root == null)
            return 0;
        if(root.left == null){
            return run(root.right)+1;
        }
        
        if(root.right == null){
            return run(root.left)+1;
        }
        int minnum = Math.min(1 + run(root.left), 1 + run(root.right));
        return minnum;
    }
}