46. Maximum Depth of Binary Tree

Easy · Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

A leaf node is a node with no children. An empty tree has a depth of 0.

Examples

Example 1
Input: [3, 9, 20, null, null, 15, 7]
Output: 3
Explanation: The tree structure is:
    3
   / \
  9  20
    /  \
   15   7
The longest path is 3 → 20 → 15 (or 3 → 20 → 7), which has 3 nodes.
Example 2
Input: [2, null, 3]
Output: 2
Explanation: The tree structure is:
  2
   \
    3
The longest path is 2 → 3, which has 2 nodes.

Constraints