直接递归就完事了,最大深度其实和求二叉树深度没啥区别,每次递归都取高度最高的子树。
class Solution {
public int maxDepth(TreeNode root) {
return dfs(root);
}
public int dfs(TreeNode root) {
if(root == null) {
return 0;
}
int left = dfs(root.left);
int right = dfs(root.right);
//高度高的子树
return Math.max(left, right) + 1;
}
}



