首页 > 其他 > 详细

LeetCode Minimum Depth of Binary Tree

时间:2014-11-12 13:46:54      阅读:252      评论:0      收藏:0      [点我收藏+]
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int minDepth(TreeNode root) {
        if(root==null){
            return 0;
        }
        if(root.left==null && root.right==null){
            return 1;
        }
        if(root.left!=null&&root.right!=null){
            return 1+<span style="color:#ff6666;">Math.min(minDepth(root.left), minDepth(root.right));</span>
        }else if(root.left!=null){
            return 1+minDepth(root.left);
        }else{
            return 1+minDepth(root.right);
        }
    }
}
在解题过程中,碰到了Time Limit Exceeded的错误,发现是由
minDepth(root.left)>minDepth(root.right)?<span style="font-family: Arial, Helvetica, sans-serif;">minDepth(root.right):minDepth(root.left)引起的,</span>
<span style="font-family: Arial, Helvetica, sans-serif;">换成上面标红:</span><pre name="code" class="java"><span style="color:#ff6666;">Math.min(minDepth(root.left), minDepth(root.right))</span>
没有错误。

LeetCode Minimum Depth of Binary Tree

原文:http://blog.csdn.net/foreverbu/article/details/41042105

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