Maximum Depth of Binary Tree
Ask both children how deep their subtrees are. The deeper side plus this node is the depth at this node. An empty child returns 0, so a leaf returns 1 + max(0, 0) = 1. The base case is the empty tree, which is 0 levels.
The depth of a subtree is 1 + the larger child depth, and an empty subtree has depth 0. That one rule covers every node, leaves included.
Problem
You are given the root of a binary tree. Return its depth: the number of nodes on the longest path from the root down to a leaf.
An empty tree has depth 0.
The root of a binary tree. The tree may be empty.
A single number: the depth of the tree.
The longest path is 3 β 20 β 15 (or 3 β 20 β 7). That path has 3 nodes.
The only path is 1 β 2, which has 2 nodes.
There are no nodes, so there are no levels.
- β’ The number of nodes in the tree is in the range [0, 10^4].
- β’ β100 β€ Node.val β€ 100
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck β each hint is counted.
- 1.
How many nodes are on a path that is just one node? What depth should that give?
- 2.
If I know the depth of the left subtree and the depth of the right subtree, can I get the depth of the whole tree?
- 3.
Only the longer side decides the longest path. Do I need both sides' numbers, or just the bigger one?
- 4.
What should an empty child contribute to the depth: 0 or 1? Why?
- 5.
Does this question count every node, or only the nodes on one longest path?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Walk the tree one level at a time with a queue; the number of levels is the depth | O(N) | O(W) |
| Optimal | Each node returns 1 + the larger of its children's answers; an empty child returns 0 | O(N) | O(H) |
Why does this belong to this pattern?
- βdepth of the treeβThe longest root-to-leaf path. It is the larger side, not the sum of the two sides.
- βroot with two subtreesβA tree is one root and two smaller trees, so the answer for the whole tree can be built from the answers for the parts.
- βempty treeβThe base case: no nodes means 0 levels.
This is the first and simplest tree question: the answer for a node comes from the answers of its children. The recursion visits the children first and combines the results on the way back up. Same Tree and Symmetric Tree keep the same walk but compare two trees instead of combining one, and that is where the traversal sub-pattern changes.
Don't make this mistake
The two empty children of a leaf would each count as one level, so a single node looks like it has a level below it. On [1] the wrong version returns 2. An empty tree returns 1 as well instead of 0. Empty means zero levels.
This counts every node, so it returns the size of the tree, not its depth. On [3, 9, 20, null, null, 15, 7] the wrong version returns 5 (the number of nodes), while the depth is 3. A depth is a longest path, so only the deeper side counts.
Adding one per popped node counts nodes. On [3, 9, 20, null, null, 15, 7] that gives 5. A round has to pop exactly one level, so the size of the queue is read once before the loop, and only then is the count increased.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.