二叉树的最小深度

public int minDepth(TreeNode root) {
    if(root==null){
        return 0;
    }

    //   1
    //  /
    // 2     
    //最小深度是 1
    if(root.right==null){
        return 1+minDepth(root.left);
    }
    //  1
    //   \
    //    2
    //的最小深度是 1
    if(root.left==null){
        return 1+minDepth(root.right);
    }
    return Math.min(minDepth(root.left),minDepth(root.right))+1;
}

https://www.mianshi.onlinehttps://www.i9code.cn

本文由博客一文多发平台 OpenWrite 发布!

逃跑的眼镜_bvbEK5
7 声望0 粉丝