const solve = (nums) => { let left = 0, right = nums.length - 1; while (left < right) { const sum = nums[left] + nums[right]; if (sum === target) return [left, right]; }}
Problem Walkthrough

Maximum Depth of Binary Tree — LeetCode 104 Walkthrough

LeetCode 104 is the simplest tree recursion problem and the perfect entry point into tree-based interview questions. Learn the DFS and BFS solutions in minutes.

6 min read|

Max Depth (#104): the simplest tree recursion problem

Three lines of code that teach the tree DFS template

Maximum Depth of Binary Tree: The Simplest Tree Recursion

If you are just starting tree problems on LeetCode, problem 104 — Maximum Depth of Binary Tree — is where you should begin. It is the most solved Easy tree problem on the platform and appears as the first problem in NeetCode's Trees section for good reason.

The maximum depth binary tree LeetCode problem teaches a recursion template that you will reuse in dozens of other tree problems. Once you internalize the pattern here, problems like Balanced Binary Tree (#110), Same Tree (#100), and Subtree of Another Tree (#572) become straightforward variations.

In this walkthrough, you will learn both the DFS recursive and BFS iterative solutions, see a visual example, and understand the edge cases interviewers expect you to handle.

Understanding the Problem — What Is Maximum Depth?

The maximum depth of a binary tree is the number of nodes along the longest path from the root node down to the farthest leaf node. Another way to think about it: the binary tree depth equals the number of levels in the tree.

Given a binary tree, return its maximum depth. That is the entire problem statement. The simplicity is what makes it a perfect teaching problem — there is no trick, no edge case buried in the description, just pure tree recursion.

For the example tree [3, 9, 20, null, null, 15, 7], the root is 3, which has two children: 9 (a leaf) and 20 (which has children 15 and 7). The longest root-to-leaf path is 3 -> 20 -> 15 (or 3 -> 20 -> 7), giving a tree height of 3.

ℹ️

Why Start Here

Maximum Depth (#104) is the most solved Easy tree problem and the first problem in NeetCode's Trees section — it teaches the recursion template used in 90% of tree problems.

DFS Recursive Solution for Maximum Depth Binary Tree

The DFS recursive approach is the canonical leetcode 104 solution and the one you should reach for first in an interview. The idea is beautifully simple: the depth of a tree is 1 plus the maximum depth of its left and right subtrees. If the node is null, return 0.

Here is the complete logic in pseudocode: if root is null, return 0. Otherwise, return 1 + max(maxDepth(root.left), maxDepth(root.right)). That is three lines of code that solve the entire problem.

The time complexity is O(n) where n is the number of nodes, because you visit every node exactly once. The space complexity is O(h) where h is the height of the tree, due to the recursion call stack. In the worst case of a skewed tree, h equals n.

This max depth recursive pattern is the foundation of tree DFS. Every tree problem that asks you to compute a property of the tree — height, diameter, balance, path sum — uses this same skeleton of recursing left, recursing right, and combining the results.

  • Base case: if node is null, return 0
  • Recursive case: return 1 + max(left depth, right depth)
  • Time: O(n) — visit every node once
  • Space: O(h) — recursion stack depth equals tree height

BFS Iterative Solution Using Level-Order Traversal

The BFS approach solves the maximum depth binary tree problem using level-order traversal. You process the tree level by level using a queue, and count how many levels you traverse. When the queue is empty, your level count is the answer.

Start by pushing the root into a queue. While the queue is not empty, process all nodes at the current level by dequeuing each node and enqueuing its children. After processing each level, increment a depth counter. When the loop ends, return the counter.

The time complexity is the same O(n) since you still visit every node. The space complexity is O(w) where w is the maximum width of the tree — in a balanced tree this is roughly n/2 at the last level. For interviews, knowing both the DFS and max depth BFS approaches shows breadth of knowledge.

  • Initialize queue with root, depth counter at 0
  • Process all nodes at current level, enqueue their children
  • Increment depth after each level completes
  • Return depth when queue is empty
⚠️

Skewed Tree Gotcha

For skewed trees (all left or all right children), depth equals n — the BFS approach uses O(1) queue space per level while DFS uses O(n) stack. Mention both in interviews.

Visual Walkthrough — Tree Height LeetCode Example

Let us trace through the example tree [3, 9, 20, null, null, 15, 7] step by step using the DFS approach. The tree has root 3 with left child 9 and right child 20. Node 20 has left child 15 and right child 7. Node 9 is a leaf.

Starting at root 3: we recurse left to node 9. Node 9 has no children, so both left and right return 0. Node 9 returns 1 + max(0, 0) = 1. Back at root 3, we recurse right to node 20. Node 20 recurses to 15 (returns 1) and 7 (returns 1). Node 20 returns 1 + max(1, 1) = 2. Finally, root 3 returns 1 + max(1, 2) = 3.

Using BFS: Level 1 has [3], Level 2 has [9, 20], Level 3 has [15, 7]. Three levels processed, depth equals 3. Both approaches confirm the tree height leetcode answer is 3.

Edge Cases Every Interview Expects

There are three edge cases for maximum depth that interviewers expect you to mention, even if they do not explicitly test them. First, the empty tree: if root is null, the depth is 0. This is your base case and handling it correctly means your solution never crashes on null input.

Second, a single node tree: if the root exists but has no children, the depth is 1. This falls out naturally from the recursion — both subtrees return 0, so the result is 1 + max(0, 0) = 1.

Third, skewed trees: a tree where every node has only a left child (or only a right child) has depth equal to n, the total number of nodes. This is the worst case for both time and space in the DFS approach, and it is worth mentioning in your complexity analysis.

  • Empty tree (root is null): return 0
  • Single node (no children): return 1
  • Skewed tree (all left or all right): depth = n, worst case for stack space
💡

Three-Line Template

The entire DFS solution: if not root return 0, return 1 + max(maxDepth(root.left), maxDepth(root.right)). Three lines. This skeleton applies to every tree DFS problem.

What Maximum Depth Teaches — The Tree DFS Template

LeetCode 104 is not just a problem to solve — it is a template to memorize. The pattern of checking for null, recursing left, recursing right, and combining results is the skeleton of nearly every tree DFS problem you will encounter in interviews.

Once you have this max depth tree DFS pattern down, you can extend it immediately. Want the minimum depth? Change max to min and add a check for non-null children. Want to check if a tree is balanced? Return -1 for unbalanced subtrees. Want the diameter? Track the sum of left and right depths at each node.

The foundation you build with this single Easy problem pays dividends across the entire Trees category. Practice it until the recursion feels automatic, then move on to Balanced Binary Tree (#110) and Diameter of Binary Tree (#543). Review the pattern regularly with YeetCode flashcards to keep the template sharp.

  • Same template applies to: Balanced Tree (#110), Same Tree (#100), Subtree (#572)
  • Extend to diameter, path sum, and invert tree problems
  • Foundation for all DFS tree problems in coding interviews
  • Use YeetCode flashcards to drill the recursion pattern through spaced repetition

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now