标签: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