LeetCode #104 Easy

Maximum Depth of Binary Tree

Maximum Depth of Binary Tree is LeetCode 104 (Easy). 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; a single node has depth 1.
  • The tree has up to 10⁴ nodes, so one visit per node is the goal.
Constraints
  • The number of nodes in the tree is in the range [0, 10⁴].
  • -100 <= Node.val <= 100
binary treedfsrecursion
Open on LeetCode ↗
02

Intuition

A node's depth is one (for the node itself) plus the depth of its deeper subtree. An empty subtree adds nothing, so it counts as 0.

That sentence is the whole solution: depth(node) = 1 + max(depth(left), depth(right)). It works because the longest path down from a node must go into one of its two subtrees, and the best it can do there is that subtree's own longest path.

A node can only answer after both children have answered, so the work happens on the way back up from the leaves. Each node is visited once.

How to spot this pattern

Reach for this shape whenever a node's answer is built from its children's answers: height, diameter, balance checks, path sums. Compute both children, combine them at the node, and return one number to the parent.

03

Approach

Try it first

Before reading on: a leaf has depth 1. If you already knew the depths of a node's left and right subtrees, what one line gives the node's depth? What should an empty child report?

1

Return 0 for an empty node

If root is null, return 0. This base case stops every branch at the bottom of the tree, and it also answers an empty input without a separate check.

2

Ask both children

Call maxDepth on the left child, then on the right child. Each call returns the depth of that whole subtree, so the current node does not need to look any further down.

3

Add one for this node

Return 1 + max(left, right). Take the larger child, not the sum: depth follows a single path, and the path can only go down one side.

4

The root's answer is the result

Leaves return 1, their parents return 2, and so on up the tree. When the call on the root returns, that number is the depth of the whole tree.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def maxDepth(self, root: Optional[TreeNode]) -> int:
▶3 if not root:
▶4 return 0
▶5 left = self.maxDepth(root.left)
▶6 right = self.maxDepth(root.right)
▶7 return 1 + max(left, right)
depth1233920157callsemptycall maxDepth on the root
nodes5each visited once
Start. The root cannot know its depth yet: it needs the depths of both subtrees first. So the calls go all the way down before any number comes back up.
depth1233920157calls3enter 3: ask left child 9
node3level 1
open calls1waiting for an answer
Enter node 3. Its depth depends on its children, so the call waits on the stack. The left child 9 is asked first; the right side waits until it answers.
depth1233920157calls39enter 9: both children empty
node9level 2
open calls2waiting for an answer
Enter node 9. Its depth depends on its children, so the call waits on the stack. Both children are empty; each empty call hits the base case and returns 0 at once.
depth12339120157calls39left0right0returns1leaf 9 returns 1
left0empty
right0empty
depth1returned to the parent
Node 9 is a leaf: both children are empty and return 0, so it returns 1 + max(0, 0) = 1, counting only itself.
depth12339120157calls320enter 20: ask left child 15
node20level 2
open calls2waiting for an answer
Enter node 20. Its depth depends on its children, so the call waits on the stack. The left child 15 is asked first; the right side waits until it answers.
depth12339120157calls32015enter 15: both children empty
node15level 3
open calls3waiting for an answer
Enter node 15. Its depth depends on its children, so the call waits on the stack. Both children are empty; each empty call hits the base case and returns 0 at once.
depth123391201517calls32015left0right0returns1leaf 15 returns 1
left0empty
right0empty
depth1returned to the parent
Node 15 is a leaf: both children are empty and return 0, so it returns 1 + max(0, 0) = 1, counting only itself.
depth123391201517calls3207enter 7: both children empty
node7level 3
open calls3waiting for an answer
Enter node 7. Its depth depends on its children, so the call waits on the stack. Both children are empty; each empty call hits the base case and returns 0 at once.
depth1233912015171calls3207left0right0returns1leaf 7 returns 1
left0empty
right0empty
depth1returned to the parent
Node 7 is a leaf: both children are empty and return 0, so it returns 1 + max(0, 0) = 1, counting only itself.
depth12339120215171calls320left1right1returns220 returns 1 + max(1, 1) = 2
left1node 15
right1node 7
depth2returned to the parent
Both children of 20 have answered: left 1, right 1. The longest path down goes into the deeper side, so it returns 1 + 1 = 2 and leaves the stack.
depth123339120215171calls3left1right2returns33 returns 1 + max(1, 2) = 3
left1node 9
right2node 20
depth3returned to the parent
Both children of 3 have answered: left 1, right 2. The longest path down goes into the deeper side, so it returns 1 + 2 = 3 and leaves the stack.
depth123339120215171callsemptyanswer3root returns 3 → maximum depth 3
maxDepth3path 3 → 20 → 15
Answer 3. The green path 3 → 20 → 15 has 3 nodes, the longest from the root to a leaf. Every node was entered once and returned once, so the work is O(n).
05

Common pitfalls

Returning 1 for an empty node

✗ Wrong
if not root:
    return 1
✓ Right
if not root:
    return 0

An empty subtree has no nodes. Returning 1 adds one to every path, so a single node would report depth 2.

Adding the children instead of taking the larger

✗ Wrong
return 1 + left + right
✓ Right
return 1 + max(left, right)

Adding counts nodes on both sides, which is closer to the diameter than the depth. Depth is the length of one path, and a path goes down only one side.

06

Edge cases

Empty tree

root is null, so the base case returns 0 straight away.

A long chain (every node has one child)

Depth equals the node count, and the recursion goes up to 10⁴ calls deep. That fits in C++ and Java; Python's default limit is 1,000, so raise it with sys.setrecursionlimit when running locally, or use the level-order version.

07

Complexity

Time
O(n)
Space
O(h)
Each node is visited once. The recursion stack holds one call per level on the current path: h is about log n for a balanced tree and n for a chain.
08

Depth, height and minimum depth

The maximum depth is the height of the root when both count nodes. Some textbooks and GFG count edges instead, so every height comes out one less; check which convention your problem uses.

MeasureCountsEmpty treeSingle node
Maximum depth (LeetCode 104)nodes on the longest root-to-leaf path01
Height of binary tree (edge convention, e.g. GFG)edges on the longest root-to-leaf path-10
Minimum depth (LeetCode 111)nodes on the shortest root-to-leaf path01
09

Maximum Depth of Binary Tree FAQ

Can the maximum depth be found without recursion?

Yes. Do a level-order traversal with a queue and count how many levels it processes; that count is the depth. It is also O(n) time, and it avoids recursion limits on a very deep tree, but the recursive version is shorter.

What are the steps?
  • Base case: an empty node returns 0.
  • Recurse: get the depth of the left and right subtrees.
  • Combine: return 1 + max(left, right).
  • Answer: the value returned for the root.
  • Complexity: O(n) time, O(h) space for the recursion stack.