题目:
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 static int minDepth(TreeNode root) {
if(root==null)
return 0;
else {
<span style="white-space:pre"> </span>return 1+Math.min(minDepth(root.left), minDepth(root.right));
}