EasyDepth-First SearchBreadth-First SearchTreeUpdated Sep 2026

104. Maximum Depth of Binary Tree

Asked at Google, Meta, Amazon, Microsoft and 4 more

Difficulty

Easy

Time

O(n)

Space

O(h)

Pattern

Depth-First Search

Companies

8

Problem

Given the root of a binary tree, return its maximum depth.

The maximum depth is the number of nodes on the longest path from the root down to the farthest leaf. An empty tree has depth 0, and a tree with only a root has depth 1.

Depth is counted in nodes, not edges

9315207level 1 → level 2 → level 3
The longest root-to-leaf path here is 3 → 20 → 15, which is 3 nodes, so the depth is 3.

Constraints

  • ●The number of nodes is in the range [0, 10^4]
  • ●-100 <= Node.val <= 100

Examples

Example 1
Inputroot = [3,9,20,null,null,15,7]
Output3
WhyThe longest path is 3 → 20 → 15 (or 3 → 20 → 7), which passes through 3 nodes.
Example 2
Inputroot = [1,null,2]
Output2
WhyThe only path is 1 → 2, so the depth is 2.
Example 3
Inputroot = []
Output0
WhyThere are no nodes at all, so the depth is 0.

Asked At

8 companies

How to Think About It

1

Recursive DFS: the depth of a node is 1 + the maximum depth of its children. Base case: null node has depth 0.

2

Why it works: you're computing depth bottom-up. A leaf returns 1. Its parent returns 1+1=2. The root gets the maximum path length.

3

BFS alternative: count the number of levels during level-order traversal. Each level adds 1 to the depth.

4

Edge cases: empty tree (depth 0), single node (depth 1), skewed tree (depth = number of nodes).

Optimal Approach

Recursive DFS:
Step 1: Base case — if root is null, return 0.
Step 2: Recurse — return 1 + max(maxDepth(left), maxDepth(right)).

The formula "1 + max(left, right)" works because:

  • The current node adds 1 to the depth
  • The deeper child determines the path length
  • The shallower child is ignored for maximum depth

Time: O(n) — visit every node once. Space: O(h) — recursion stack where h is tree height.

Each node returns 1 + its deeper child

9leaf → 131 + max(1, 2) = 315201 + max(1, 1) = 27null → 0leaf → 1parent → 1 + max(left, right)
The leaves answer 1, and every parent adds one more. The root ends up with the length of the longest path.
ComplexityTime O(n)Space O(h)Every node is visited once. h is the height of the tree — the depth of the recursion stack.

What Trips People Up in Real Interviews

1

Overcomplicating this. The depth is simply 1 + max(depth(left), depth(right)). Don't overthink it.

2

Forgetting the base case: a null node has depth 0. Without this, you'll get infinite recursion.

3

Confusing "depth" with "height." They're the same for binary trees — the number of nodes on the longest path from root to leaf.

4

Not handling an empty tree. Return 0, not -1 or null.

5

Counting edges instead of nodes. Depth is the number of nodes on the longest root-to-leaf path, not the number of edges. A single-node tree has depth 1, not 0.

Solution Code

O(n) time
def maxDepth(root):
    if not root:
        return 0
    return 1 + max(maxDepth(root.left), maxDepth(root.right))

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Maximum Depth of Binary Tree problem?

Given the root of a binary tree, return its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

How do you solve Maximum Depth of Binary Tree?

The optimal approach runs in O(n) time and O(h) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Maximum Depth of Binary Tree?

Maximum Depth of Binary Tree is asked at Google, Meta, Amazon, Microsoft, Apple, Netflix, Uber, LinkedIn. It is a easy difficulty problem.

What are common mistakes on Maximum Depth of Binary Tree?
  • Overcomplicating this. The depth is simply 1 + `max(depth(left)`, depth(right)). Don't overthink it.
  • Forgetting the base case: a `null` node has depth 0. Without this, you'll get infinite recursion.
  • Confusing "depth" with "height." They're the same for binary trees — the number of nodes on the longest path from root to leaf.
  • Not handling an empty tree. Return 0, not -1 or `null`.
  • Counting edges instead of nodes. Depth is the number of nodes on the longest root-to-leaf path, not the number of edges. A single-node tree has depth 1, not 0.