LeetCode 104: Maximum Depth of Binary Tree – Java Recursive Solution Explained

Learn how to solve LeetCode 104 Maximum Depth of Binary Tree using recursion and DFS in Java. Includes brute force intuition, optimized recursive approach, BFS solution, dry run, complexity analysis, interview explanation, and common mistakes.

Krishna Shrivastava
2 views
LinkedInGithubX
0
0
LeetCode 104: Maximum Depth of Binary Tree – Java Recursive Solution Explained
Listen to articleAudio version
Ad
Advertisement

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:

  1. Binary Tree Traversal
  2. Depth First Search (DFS)
  3. Recursion
  4. Tree Height Calculation
  5. Divide and Conquer

It is one of the most frequently asked tree questions in coding interviews because it builds the foundation for:

  1. Tree recursion
  2. Height problems
  3. Balanced tree problems
  4. Diameter problems
  5. 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 of the tree

Maximum depth means:

Number of nodes along the longest path from root to the farthest leaf node.

Example 1

Input

root = [3,9,20,null,null,15,7]

Tree:

3
/ \
9 20
/ \
15 7

Output

3

Explanation:

Longest path:

3 → 20 → 15

contains:

3 nodes

Example 2

Input

root = [1,null,2]

Tree:

1
\
2

Output:

2

Understanding Maximum Depth

Depth means:

How many levels exist in the tree

For example:

1
/ \
2 3
/
4

Levels:

Level 1 → 1
Level 2 → 2,3
Level 3 → 4

Maximum depth:

3

Key Observation

The depth of a tree depends on:

Maximum depth of left subtree
and
Maximum depth of right subtree

So:

Depth(root)
=
1 + max(leftDepth, rightDepth)

This is the core recursive formula.

Recursive Intuition

At every node:

  1. Find depth of left subtree
  2. Find depth of right subtree
  3. Take maximum
  4. Add current node

This naturally becomes a recursive DFS problem.

Java Recursive Solution

class Solution {

public int maxDepth(TreeNode root) {

if(root == null)
return 0;

int left = maxDepth(root.left);

int right = maxDepth(root.right);

return 1 + Math.max(left, right);
}
}

Why This Works

At every node:

  1. Recursively calculate left depth
  2. Recursively calculate right depth
  3. Choose bigger depth
  4. Add:
1

for current node.

This continues until leaf nodes.

Dry Run

Input

3
/ \
9 20
/ \
15 7

Step 1

Start from root:

3

Step 2

Left subtree:

9

Depth:

1

Step 3

Right subtree:

20

Its children:

15 and 7

Depth becomes:

2

Step 4

At root:

1 + max(1,2)

Result:

3

Recursive Call Flow

maxDepth(3)
├── maxDepth(9)
│ ├── 0
│ └── 0
└── maxDepth(20)
├── maxDepth(15)
└── maxDepth(7)

Then values return upward.

Alternative BFS Approach

We can also solve this using:

Level Order Traversal

using a queue.

Every level increases depth by:

1

BFS Java Solution

class Solution {

public int maxDepth(TreeNode root) {

if(root == null)
return 0;

Queue<TreeNode> queue = new LinkedList<>();

queue.offer(root);

int depth = 0;

while(!queue.isEmpty()) {

int size = queue.size();

for(int i = 0; i < size; i++) {

TreeNode node = queue.poll();

if(node.left != null)
queue.offer(node.left);

if(node.right != null)
queue.offer(node.right);
}

depth++;
}

return depth;
}
}

DFS vs BFS

ApproachTechniqueSpace
DFSRecursionO(H)
BFSQueueO(N)

Time Complexity Analysis

Recursive DFS Solution

Time Complexity

O(N)

because every node is visited once.

Space Complexity

O(H)

where:

H = tree height

Worst case:

O(N)

for skewed tree.

BFS Solution

Time Complexity

O(N)

Space Complexity

O(N)

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:

  1. Tree recursion understanding
  2. DFS traversal knowledge
  3. Divide and conquer thinking

Common Mistakes

1. Forgetting Base Case

Always handle:

if(root == null)
return 0;

2. Using Min Instead of Max

We need:

Longest path

not shortest.

3. Incorrect Depth Counting

Remember to add:

1

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:

  1. Minimum Depth of Binary Tree
  2. Balanced Binary Tree
  3. Diameter of Binary Tree
  4. Binary Tree Level Order Traversal
  5. Path Sum

Conclusion

LeetCode 104 is one of the most important beginner binary tree problems.

It teaches:

  1. Recursive DFS
  2. Tree height calculation
  3. Divide and conquer
  4. 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.

Ai Assistant Kas