Pattern this teaches:Tree DFS
Maximum Depth of Binary Tree
An easy Binary Trees interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Binary Trees
- Sheets
- 3
- Core for
- 4 roles
The problem
Work out how tall a binary tree is: count the nodes on the longest route that starts at the root and ends at a leaf. An empty tree has depth 0.
Example 1
- Input
- [4,2,6,1,3,5,7]
- Output
- 3
- Why
- Full binary tree of height 3: 4->2->1 and 4->6->5 both depth 3.
Example 2
- Input
- [6,null,3]
- Output
- 2
- Why
- The root and its right child make a depth of 2.
Example 3
- Input
- []
- Output
- 0
- Why
- An empty tree has depth 0.
Constraints
- The number of nodes is in the range [0, 104].
- -100 <= Node.val <= 100
How to think about it
Updated 2026-09-29The depth of any tree is one plus the larger depth of its two subtrees. You do not need to trace or compare whole paths down to individual leaves; depth is an inductive quantity synthesized entirely at the return step of each subproblem.
Approaches, worst first
Level order counting
time O(n) · space O(w)
Run breadth-first search using a queue, incrementing a depth counter after exhausting all nodes in each level snapshot. Correct, but allocates a queue that holds the entire tree width in memory.
Postorder bottom-up recursionWrite this one
time O(n) · space O(h)
Return 0 for null. For any valid node, compute depths of left and right subtrees recursively and return 1 + max(left, right). It requires no helper data structures and fits in three lines.
Where people lose marks · 3
- Confusing depth in nodes with depth in edges; a single-node tree has depth 1, while an empty tree has depth 0.
- In a BFS level-order approach, not snapshotting the queue length before the inner loop causes newly pushed children to merge into the current level.
- Stack overflow can occur on pathological single-spine trees if recursion is unchecked on deep inputs.
Full solution
Postorder bottom-up recursion: depth is synthesized at the return step from the two subtrees, no queue or explicit stack needed.
Python
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def max_depth(root: TreeNode | None) -> int:
if root is None:
return 0
# depth of a tree = 1 + the larger depth of its two subtrees
return 1 + max(max_depth(root.left), max_depth(root.right))
JavaScript
class TreeNode {
constructor(val = 0, left = null, right = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
function maxDepth(root) {
if (root === null) return 0;
// depth of a tree = 1 + the larger depth of its two subtrees
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
The theory behind it
Binary Trees — the ground this problem stands on. All Binary Trees problems
What Binary Trees is
A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.
When to reach for it
Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.
How the pattern works
Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.
More Binary Trees problems
Target Roles
Core requirement for 4 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .