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

LeetCode Minimum Depth of Binary Tree

时间:2014-11-12 13:49:37      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:style   io   color   ar   java   sp   for   on   amp   

/**
 * 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

标签:style   io   color   ar   java   sp   for   on   amp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!