码迷,mamicode.com
首页 > 其他好文 > 详细

Minimum Depth of Binary Tree

时间:2015-06-30 06:34:15      阅读:138      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!