Introduction
LeetCode 104 – Maximum Depth of Binary Tree is one of the most important beginner tree problems in Data Structures and Algorithms.
This problem teaches:
- Binary Tree Traversal
- Depth First Search (DFS)
- Recursion
- Tree Height Calculation
- Divide and Conquer
It is one of the most frequently asked tree questions in coding interviews because it builds the foundation for:
- Tree recursion
- Height problems
- Balanced tree problems
- Diameter problems
- DFS traversal
If you are starting binary trees, this is one of the best problems to master first.
Problem Link
🔗 https://leetcode.com/problems/maximum-depth-of-binary-tree/
Problem Statement
Given the root of a binary tree:
Return:
Maximum depth means:
Number of nodes along the longest path from root to the farthest leaf node.
Example 1
Input
Tree:
Output
Explanation:
Longest path:
contains:
Example 2
Input
Tree:
Output:
Understanding Maximum Depth
Depth means:
For example:
Levels:
Maximum depth:
Key Observation
The depth of a tree depends on:
So:
This is the core recursive formula.
Recursive Intuition
At every node:
- Find depth of left subtree
- Find depth of right subtree
- Take maximum
- Add current node
This naturally becomes a recursive DFS problem.
Java Recursive Solution
Why This Works
At every node:
- Recursively calculate left depth
- Recursively calculate right depth
- Choose bigger depth
- Add:
for current node.
This continues until leaf nodes.
Dry Run
Input
Step 1
Start from root:
Step 2
Left subtree:
Depth:
Step 3
Right subtree:
Its children:
Depth becomes:
Step 4
At root:
Result:
Recursive Call Flow
Then values return upward.
Alternative BFS Approach
We can also solve this using:
using a queue.
Every level increases depth by:
BFS Java Solution
DFS vs BFS
| Approach | Technique | Space |
| DFS | Recursion | O(H) |
| BFS | Queue | O(N) |
Time Complexity Analysis
Recursive DFS Solution
Time Complexity
because every node is visited once.
Space Complexity
where:
Worst case:
for skewed tree.
BFS Solution
Time Complexity
Space Complexity
queue may contain full level.
Interview Explanation
In interviews, explain:
The depth of a node depends on the maximum depth between its left and right subtree. This naturally forms a recursive divide-and-conquer problem.
This demonstrates:
- Tree recursion understanding
- DFS traversal knowledge
- Divide and conquer thinking
Common Mistakes
1. Forgetting Base Case
Always handle:
2. Using Min Instead of Max
We need:
not shortest.
3. Incorrect Depth Counting
Remember to add:
for current node.
FAQs
Q1. What is maximum depth?
It is the number of nodes in the longest root-to-leaf path.
Q2. Why is recursion preferred?
Tree problems naturally fit recursive structures.
Q3. Can this be solved iteratively?
Yes.
Using BFS with queue.
Q4. Is this problem important for interviews?
Very important.
It is one of the most fundamental tree recursion problems.
Related Problems
After mastering this problem, practice:
- Minimum Depth of Binary Tree
- Balanced Binary Tree
- Diameter of Binary Tree
- Binary Tree Level Order Traversal
- Path Sum
Conclusion
LeetCode 104 is one of the most important beginner binary tree problems.
It teaches:
- Recursive DFS
- Tree height calculation
- Divide and conquer
- Binary tree traversal
The key insight is:
Maximum depth equals 1 + maximum depth of left and right subtree.
Once this recursive pattern becomes clear, many advanced tree problems become easier to solve.





