原题:点击此处
考点:DFS、二叉树
这道题很简单,不多描述
class Solution {
public int maxDepth(TreeNode root) {
return maxDepth(root,0,0);
}
public int maxDepth(TreeNode root,int depth,int max){
if(root == null){
return depth;
}
int leftDepth = maxDepth(root.left,depth,max);
int rightDepth = maxDepth(root.right,depth,max);
depth = Math.max(leftDepth,rightDepth)+1;
max = Math.max(depth,max);
return depth;
}
}