首页 > 其他 > 详细

Minimum Depth of Binary Tree

时间:2015-06-30 06:33:49      阅读:234      评论:0      收藏:0      [点我收藏+]

http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ 

 

This question is pretty similar to the solution of Maximum Depth of Binary Tree, the only difference is that apart from the if condition (base case) to determine wheter the current Node is Null , two additional conditions are needed to determine whether the child Nodes are Null , because only from the Child Nodes can well determine whether they are leaf nodes or not.

 

Solution:

  public int minDepth(TreeNode root) {
        if (root == null) return 0;
        if (root.left == null) return 1 + minDepth(root.right);
        if (root.right == null) return 1 + minDepth(root.left);
        else {
            return 1+Math.min(minDepth(root.left),minDepth(root.right));
        } 
    }

 

Minimum Depth of Binary Tree

原文:http://www.cnblogs.com/midan/p/4609313.html

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