自己的代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int count = 0;
public int maxDepth(TreeNode root) {
if(root != null){
count++;
return maxDepth(root.left)>maxDepth(root.right)?maxDepth(root.left);
}else{
return count;
}
}
}
正确代码
if (root == null) {
return 0;
}
result = Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
return result;
}
知识
Math.max取最大