Minimum Depth of Binary Tree

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.

 1 public class Solution {
 2     public int minDepth(TreeNode root) {
 3         if(root==null) return 0;
 4         int left = minDepth(root.left);
 5         int right = minDepth(root.right);
 6         if(left==0 && right ==0) return 1;
 7         if(left ==0) left=Integer.MAX_VALUE;
 8         if(right==0) right=Integer.MIN_VALUE;
 9         return Math.min(right,left)+1;
10     }
11 }
View Code

 

posted @ 2014-02-06 14:53  krunning  阅读(100)  评论(0编辑  收藏  举报