WorkspaceProblem lab
ALGORITHM VISUALIZER

Maximum Depth of Binary Tree

Opening the interactive workspace…

Understanding Tree Maximum Depth

Return the number of nodes on the longest path from the root of a binary tree to a leaf. The input uses level-order values with null for missing children.

How it works

The answer at each node depends only on the depths returned by its two children.

The base case returns zero for null. Each non-null call returns one plus the larger child depth. The call stack stores unfinished parent work.

Try an example

Input: values = [3, 9, 20, null, null, 15, 7]

Output: 3

The path 3 → 20 → 15 contains three nodes.

Constraints

  • An empty tree has depth 0.
  • A single-node tree has depth 1.
  • Visualization limit: 63 level-order entries.

Use the simulator

Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.

Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.

Related concepts and complexity