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;
int Left = minDepth(root.left);
int Right = minDepth(root.right);
if(Left==0&&Right==0)
return 1;
if(Left==0)
Left = Integer.MAX_VALUE;
if(Right==0)
Right = Integer.MAX_VALUE;
return (Left>Right)? (Right+1):(Left+1);
}
}
网友评论