LeetCode104 Maximum Depth of Binary Tree
idea: the maxDepth of current Node is the maxDepth between its left subtree and right subtree plus 1, so it’s max(l, r) + 1.
class Solution {
public int maxDepth(TreeNode root) { //the maxDepth under current node as rude, think this straight and it's very easy
if(root == null) return 0;
int l = maxDepth(root.left);
int r = maxDepth(root.right);
return Math.max(l, r) + 1;
}
}
and of course we can use BFS to get the max depth.

浙公网安备 33010602011771号