【发布时间】:2016-01-24 07:27:29
【问题描述】:
public class Solution {
public int maxDepth(TreeNode root) {
if(root==null) return 0;
else
return (maxDepth(root.left)>maxDepth(root.right))?(maxDepth(root.left)+1):(maxDepth(root.right)+1);
}
}
它返回超过时间限制。我想知道为什么会发生这种情况,我的代码有什么问题?
【问题讨论】:
-
使用主定理并假设树是平衡的,运行时间在
Omega(n^1.58),但你可以达到O(n)(除非我忽略了一些其他错误)