阿里面试真题:给定一个二叉树,找出其最小深度

给定一个二叉树,找出其最小深度。 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

参考文档:https://juejin.im/post/5b8d64346fb9a01a1d4f99fa

class Solution {
    public int minDepth(TreeNode root) {
        if(root == null)
            return 0;
        int left = minDepth(root.left);
        int right = minDepth(root.right);
        return (left == 0 || right == 0) ? left + right + 1 : Math.min(left, right) + 1;
    }
}

如果非递归就更好了。。

发布了224 篇原创文章 · 获赞 100 · 访问量 53万+

猜你喜欢

转载自blog.csdn.net/chenpeng19910926/article/details/103800418