Skip to content
Optimal Lab

Trees · 025Easy

Maximum Depth of Binary Tree

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

Constraints

  • The tree holds 0 to 10^4 nodes.
  • -100 <= Node.val <= 100
  • INPUT FORMAT
  • `root` is a TreeNode object, not an array. The arrays in EXAMPLES are the level-order
  • serialization the platforms print — breadth-first, with `null` for a missing child. The
  • test runner builds a real tree from that array before calling your function.

Examples

maxDepth([3, 9, 20, null, null, 15, 7])  ->  3
maxDepth([1, null, 2])                   ->  2
maxDepth(null)                           ->  0
maxDepth([1])                            ->  1   // a single node has depth 1
maxDepth([1, 2, 3, 4, 5, 6, 7])          ->  3   // perfectly balanced
maxDepth([1, 2, null, 3, null, 4])       ->  4   // fully left-skewed, depth equals n

Edge cases

  • Empty tree returns 0, not 1.
  • A single node has depth 1.
  • A degenerate (skewed) tree of 10^4 nodes can exhaust the recursion stack.
Show the target complexity (spoiler)
Naive
None — a full traversal is required either way.
Target
O(n) time, O(h) space — 1 + max(depth(left), depth(right)), or a level-counting BFS.

Solve it

By hand, in your editor. The runner checks answers and timing.

git clone https://github.com/sarmadkung/optimal-round
cd optimal-round
# open problems/07-trees/025-maximum-depth-of-binary-tree.js
./practice c 025

Stuck? Watch the pattern run