Back to Practice
#0275

Maximum Depth of Binary Tree

EasyDSA18 min10 XP

Problem

A tree node is [value, left, right], and an empty child is None. Return the maximum depth of the tree.

Why This Matters

Tree depth is the simplest recursive tree problem and the base for balance, diameter, and path questions.

Function Signature

def max_depth(root):

Examples

Example 1
Inputroot = [3, [9, None, None], [20, [15, None, None], [7, None, None]]]
Output3

The longest path is 3 -> 20 -> 15 or 3 -> 20 -> 7.

Constraints

  • Return the exact requested value.
  • Handle edge cases cleanly.
  • Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Three levels
Inputbalanced-ish tree
Expected3

The deepest leaf is three nodes from the root.

Empty tree
InputNone
Expected0

No nodes means depth zero.

Hidden Test Categories
single nodeleft-heavy treeright-heavy tree