您的位置:首页 > 其它

97 - 二叉树的最大深度

2017-03-30 17:39 162 查看
3.30

刚开始用三目表达式就超时了

慎用啊

一定要避免重复计算

/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: An integer.
*/
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
if(root.right == null){
return maxDepth(root.left)+1;
}
if(root.left == null){
return maxDepth(root.right)+1;
}
int x1 = maxDepth(root.left);
int x2 = maxDepth(root.right);
if(x1 > x2){
return x1+1;
}
else{
return x2+1;
}
// return maxDepth(root.left) > maxDepth(root.right)?maxDepth(root.left)+1:maxDepth(root.right)+1;
// write your code here
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: