Skip to content
AI360Xpert
Beta
Difficulty: EasyTrees

Maximum Depth of Binary Tree

Problem in Plain English

Given the root of a binary tree, return the number of nodes on the longest path from the root down to a leaf. An empty tree has depth 0.

Problem Statement

Given the root of a binary tree, return its maximum depth.

The maximum depth is the number of nodes along the longest downward path from the root to any leaf. An empty tree has depth 0, and a single node has depth 1.

Constraints

  • 0≤n≤1040 \le n \le 10^4, where nn is the number of nodes.
  • −100≤Node.val≤100-100 \le \text{Node.val} \le 100.

Examples

Example 1

Input
root = [3,9,20,null,null,15,7]
Output
3

The path 3 to 20 to 15 holds 3 nodes, which beats the 2-node path through 9.

Example 2

Input
root = [1,null,2]
Output
2

The missing left child does not stop the path through the right child: 1 to 2 contains two nodes. A minimum of the child depths would incorrectly discard this path.

Example 3

Input
root = []
Output
0

Empty edge case: there is no root and no root-to-leaf path, so zero nodes contribute to the depth.

Example 4

Input
root = [0,-1,-1,null,null,-1,null,-1]
Output
4

Duplicate negative values denote distinct nodes. The right subtree continues down a left-child chain, giving the four-node path with values 0, -1, -1, -1. Values and signs do not determine depth.

Intuition

Depth counts nodes on one downward path, not edges or values. In Example 1, the path from 3 through 20 to 15 has three nodes, while the path to 9 has two. Count nonempty horizontal levels, or compute each child subtree depth and add one to the larger answer. An empty child contributes zero, making a leaf contribute one; taking a maximum chooses one path rather than combining both branches.

Approaches

Level Count With BFS

Solution Details

Reveal Level Count With BFS: intuition, complexity, and code

Hints

Hint 1
Does changing a node value change the number of nodes on a path? Focus on child links.
Hint 2
An empty subtree contributes zero. What should a leaf return?
Hint 3
A downward path chooses one child: take the larger child depth and add the current node.
Hint 4
Alternatively, count nonempty breadth-first frontiers, keeping children separate from the current level.

Edge Cases

  • An empty tree returns 0; a singleton returns 1, including zero or negative values.
  • Left-only, right-only, and alternating chains have depth n; at 10,000 nodes BFS remains independent of recursive stack capacity.
  • A missing child contributes zero without stopping the existing child path.
  • A complete tree has modest depth but a wide BFS frontier; recursion stores only one root-to-leaf path.
  • Duplicate values do not identify nodes. Neither approach changes values or child references.

Common Mistakes and Interview Tips

  • Counting edges instead of nodes: use zero for null and add one for every nonempty node.
  • Adding both child depths combines separate paths; taking their minimum discards longer paths, especially when one child is missing.
  • Appending children to the array being iterated mixes levels. Collect them in a separate next frontier.
  • Repeated JavaScript shift() may move remaining array entries and make wide-tree traversal quadratic.
  • Assuming default stacks support all legal chains: Java and JavaScript can overflow at 10,000 nodes. Provision stack capacity or choose BFS. Python recursion limits are process-wide and should not be changed concurrently by other threads.
  • Retaining a global depth counter across invocations gives stale answers; keep traversal state local.

Key Takeaway

Height is a post-order summary: empty contributes zero, and each parent adds itself to the larger child answer. Breadth-first level counting computes the same quantity outward from the root. Choose between width-dependent frontier storage and height-dependent recursive storage according to tree shape and runtime stack capacity.