/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxDepth(TreeNode root) {
if(null == root){
return 0;
}
return 1 + Math.max(maxDepth(root.left),maxDepth(root.right));
}
}
本题在leetcode中若使用
return 1 + maxDepth(root.left)>maxDepth(root.right)?maxDepth(root.left):maxDepth(root.right); 会出现Timeout。可能是由于递归层次太深导致。