But the AC answer shows that if there is only one root, maximum depth of this tree is 1. Need to discuss about this with my friend.
/**
* 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(root == null) return 0;
int maxDepthVal = 0;
maxDepthVal = Math.max(maxDepthVal, Math.max(maxDepth(root.right),maxDepth(root.left)) + 1 );
return maxDepthVal;
}
}
No comments:
Post a Comment