public class Binarytreedept {
/*
 * 输出二叉树最小深度   
 * 核心思想:根节点到达最近的叶子节点的路径长度。
 * 1、当根为空时,输出0。
 * 2、当左子树为空时,输出右子树深度+1。
 * 3、当右子树为空时,输出左子树深度+1。
 * 4、以上条件都不满足时,输出min(左子树深度,右子树深度)+1。
 * 
 * 输出二叉树最大深度 
 * 核心思想:根节点到达最远的叶子节点的路径长度。
 * 1、如果二叉树为空,则深度为0;
 * 2、如果不为空,分别求左子树的深度和右子树的深度,取较大值加1,因为根节点深度是1。
 */
    public static int minDepth(BiTreeNode root) {
        //如果二叉树为空,返回0;
        if(root == null)
            return 0; 
        //如果二叉树左子树为空,返回右子树深度;
        if(root.lchild == null) 
        return minDepth(root.rchild) + 1;
        //如果二叉树右子树为空,返回左子树深度;
        if(root.rchild == null) 
        return minDepth(root.lchild) + 1;
        //如果二叉树左右子树均不为为空,返回左右子树深度的较小值;
        int leftDepth = minDepth(root.lchild);
        int rightDepth = minDepth(root.rchild);
        return leftDepth < rightDepth ? (leftDepth + 1) : (rightDepth + 1);
    }
    public static int maxDepth(BiTreeNode root) {
        //如果二叉树为空,返回0;        
        if(root == null)
            return 0;
        //否则返回二叉树左右子树深度较大值;  
        int leftDepth = maxDepth(root.lchild);
        int rightDepth = maxDepth(root.rchild);
        return leftDepth > rightDepth ? (leftDepth + 1) : (rightDepth + 1);
    }
}