Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
这个算法的难点就是,要判断左边或右边是否为空,因为如果一边为空,它的深度肯定是小于另外一边,但是此时为空的算是没有深度了,所以最小深度要为另一边不为空的。大家看下我最开始写的算法,始终是超时,我真是百思不得其解。
public int minDepth(TreeNode root) {
if (root == null)
return 0;
if (root.left == null && root.right ==

该博客主要讨论LeetCode中111题的解决方案,即找到二叉树的最小深度。博主分享了自己最初遇到的算法超时问题,以及如何通过判断节点是否为空来优化算法,最终实现正确且高效地找出从根节点到最近叶子节点的最短路径长度。
订阅专栏 解锁全文
175

被折叠的 条评论
为什么被折叠?



