首页 > 其他 > 详细

Minimum Depth of Binary Tree

时间:2014-02-06 16:26:58      阅读:362      评论:0      收藏:0      [点我收藏+]

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.

bubuko.com,布布扣
 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

Minimum Depth of Binary Tree

原文:http://www.cnblogs.com/krunning/p/3538789.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!