TRT
Sign in
easyCoreLeetCode#bottom-up recursion#level order

Maximum depth of binary tree

Return the number of nodes on the longest root-to-leaf path, recursively and with a level-order traversal.

The Runtime Theory Team1 min read
Taught inVocabulary and traversalsPro — members onlyA tree is nodes joined by parent-child links with no cycles. Visiting every node is a choice of order: before the children, between them, or after them. Each order fits different jobs.
Solve it

Solving happens on the judge — come back and mark it done

Sample cases

inroot = [3, 9, 20, null, null, 15, 7]

out3

inroot = [1, null, 2]

out2

inroot = []

out0

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.

Before you code

The depth of a tree is one more than the deeper of its two subtrees, and an empty tree has depth 0. That is the whole recursive solution.

Hints

  1. depth(None) = 0; depth(node) = 1 + max(depth(left), depth(right)).
  2. Iterative version: count the levels of a level-order traversal.
  3. Note that this problem counts nodes, so a single node has depth 1; the lesson counts edges. Say which you mean.

Production & complexity notes

When solving Maximum depth of binary tree on LeetCode (easy), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Vocabulary and traversals to reinforce the underlying pattern.

What it practices

Recursion on subtrees, from Vocabulary and traversals.

Not started

Sign in to save your learning progress.

Sign in to save