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.
中文:给一二叉树,找出其最小深度。
最小深度是从根节点到最近的叶子节点的最短路径经过的节点的数量。
Java:
public int minDepth(TreeNode root) {
if (root == null)
return 0;
int a = minDepth(root.left);
int b = minDepth(root.right);
if (a == 0)
return b + 1;
if (b == 0)
return a + 1;
return Math.min(a, b) + 1;
}
342

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



