Given the root of a binary tree, return its maximum depth (the number of nodes along the longest path from root to the farthest leaf).
Example 1
Input: root = [3,9,20,null,null,15,7]
Output: 3
Explanation: The longest path is 3 -> 20 -> 15 (or 7), 3 nodes deep.
The number of nodes is in the range [0, 10^4]A tree's depth is naturally defined recursively: 1 + the deeper of its two subtrees' depths.
The base case (null node) has depth 0 — this is what makes the recursion terminate correctly.
public int maxDepth(TreeNode root) {
if (root == null) return 0;
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}Time: O(n) · Space: O(h)