Maximum Depth of Binary Tree

easy

Given the root of a binary tree, return its maximum depth — the number of nodes along the longest path from the root down to the farthest leaf.

The tree is given in level-order (breadth-first), using null for missing children.

Hints

The depth of a tree is one more than the depth of its deeper subtree.
The base case is an empty tree, whose depth is 0.
Alternatively, count how many levels a breadth-first traversal processes.

Common doubts

In nodes: an empty tree is 0, a single node is 1, and each level adds one.
The parent's depth depends on both children's depths, so you must resolve the children first — bottom-up.
For pathologically deep (skewed) trees where recursion could overflow the call stack.

Interview follow-ups

Take the min of the subtree depths, but be careful: a node with one missing child must use the present child's depth, not 0 (LeetCode 111).
Balance uses the same height computation but also compares the two subtree heights at every node.

Fun facts

  • Maximum depth equals the number of levels a breadth-first traversal produces.
  • This recurrence is the seed of many tree DPs: height, balance, diameter, and path sums all extend it.

Asked at

AmazonMicrosoftLinkedIn
Frequently Sometimes Occasionally
Example 1
Input: root = [3, 9, 20, null, null, 15, 7]
Output: 3
The longest root-to-leaf path 3 -> 20 -> 15 (or 3 -> 20 -> 7) has 3 nodes.
Example 2
Input: root = [1, null, 2]
Output: 2
The path 1 -> 2 has 2 nodes.
Constraints

- The number of nodes is in the range [0, 10^4] - -100 <= Node.val <= 100

Solve this problem →