Maximum Depth of a Binary Tree
Table of Contents + β
When an interviewer asks for the maximum depth of a binary tree, they are not really testing if you know what βdepthβ means. They want to see if you can think about a tree the way it is actually built. A tree is a small piece that contains more small pieces just like it. So this question is a quiet test of recursion. If you can see the tree as βa node sitting on top of two smaller treesβ, you have already solved it.
Let us walk through it the way you would in the room.
π³ The Problem
You are given the root of a binary tree. A binary tree is a structure where every node has at most two children: a left child and a right child. You need to return its maximum depth.
The maximum depth is the number of nodes on the longest path going from the root down to the farthest leaf. A leaf is a node with no children. An empty tree has depth 0.
Here is a small example:
Input: 3 / \ 9 20 / \ 15 7
Output: 3See why the answer is 3? The longest path is 3 β 20 β 15 (or 3 β 20 β 7). That path touches 3 nodes. The path on the left, 3 β 9, touches only 2 nodes. So the deepest one wins, and the answer is 3.
π€ The Simple Idea First
One way people first think about this is to walk the whole tree and keep a counter of βhow deep am I right nowβ. Then you track the biggest value of that counter. That works, but you have to carry an extra depth variable down through every call. You also have to remember to update a separate maximum. It is easy to make a mistake here, and in an interview you can lose track of the values.
There is a cleaner way to say the same thing. Instead of pushing a counter downward, let each node ask its children βhow deep are you?β and build the answer coming back up.
β The Recursive Approach
Here is the key thought. The depth of any node is just 1 plus the depth of its taller child. The 1 is for the node itself. Then you add whichever side goes deeper, the left subtree or the right subtree.
And what about an empty spot, where there is no node at all? Its depth is 0. That is the base case that stops the recursion. So the whole rule fits in two lines:
- If the node is empty (null), return 0.
- Otherwise return
1 + max(depth(left), depth(right)).
That is it. Every node solves a tiny version of the same problem, and the answers stack up to give the full height.
Tip
Notice we never pass a counter down. Each call returns the depth of the subtree below it, so the parent just adds 1. Letting recursion return values up the call stack is usually cleaner than carrying state down.
Steps to find the maximum depth
- Start at the root node.
- If the current node is null, return 0. This is the base case.
- Recursively find the depth of the left subtree.
- Recursively find the depth of the right subtree.
- Take the larger of the two depths.
- Add 1 for the current node and return that value.
Let us build the example tree and print its depth.
# A node of the binary treeclass Node: def __init__(self, val): self.val = val self.left = None self.right = None
# Depth = 1 + taller child's depth; empty node is 0def max_depth(root): if root is None: return 0 return 1 + max(max_depth(root.left), max_depth(root.right))
# Build the tree: 3 / (9, 20) / (15, 7)root = Node(3)root.left = Node(9)root.right = Node(20)root.right.left = Node(15)root.right.right = Node(7)
print("Maximum depth:", max_depth(root))The output of the above code will be:
Maximum depth: 3β±οΈ Time and Space Complexity
We visit every node exactly once, so time is O(n) where n is the number of nodes. The space comes from the recursion call stack, which goes as deep as the height of the tree.
| Approach | Time | Space |
|---|---|---|
| Recursion (this solution) | O(n) | O(h) |
| Carrying a depth counter down | O(n) | O(h) |
Here h is the height of the tree. In the worst case the tree is one long slanted chain, so h equals n and the space becomes O(n). For a balanced tree h is much smaller, about log n.
Note
If the interviewer says the tree could be very deep, mention that deep recursion can overflow the call stack. You can then offer an iterative version using a stack or a level-by-level queue (BFS). Naming that trade-off shows you understand the choices involved.
π§© Key Takeaways
- β
The depth of a node is
1 + max(left depth, right depth). The empty node returns 0 and stops the recursion. - β Let recursion return answers up the stack instead of passing a counter down. It is cleaner and easier to reason about.
- β Time is O(n) because every node is visited once.
- β Space is O(h), the height of the tree, because of the recursion stack. A skewed tree pushes this to O(n).
- β If depth could be huge, an iterative BFS or stack-based version avoids stack overflow.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What value should the function return for an empty (null) node?
Why: An empty subtree contributes no nodes to any path, so its depth is 0, and this is the base case that stops the recursion.
- 2
How is the depth of a non-empty node computed from its children?
Why: You add 1 for the node itself and take the larger of the two child depths, because the deepest path decides the height.
- 3
What is the time complexity of this recursive solution?
Why: Each node is visited exactly once, so the work is proportional to the number of nodes, n.
- 4
Why is the space complexity O(h) and not O(1)?
Why: The recursion stack holds one frame per level on the current path, so its depth equals the tree's height h.