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 class Solution {
public int minDepth(TreeNode root) {
if(root==null)return 0;
if(root.left==null&&root.right==null)return 1;
if(root.left==null)return 1+minDepth(root.right);
else{
if(root.right==null)return 1+minDepth(root.left);
else return 1+Math.min(minDepth(root.right),minDepth(root.left));
}
}
}