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.
- The number of nodes in the tree is in the range [0, 10⁴].
- -100 <= Node.val <= 100
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.
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.
Approach
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?
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.
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.
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.
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.
Code in Python, C++ and Java
Common pitfalls
Returning 1 for an empty node
if not root:
return 1if not root:
return 0An 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
return 1 + left + 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.
Edge cases
root is null, so the base case returns 0 straight away.
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.
Complexity
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.
| Measure | Counts | Empty tree | Single node |
|---|---|---|---|
| Maximum depth (LeetCode 104) | nodes on the longest root-to-leaf path | 0 | 1 |
| Height of binary tree (edge convention, e.g. GFG) | edges on the longest root-to-leaf path | -1 | 0 |
| Minimum depth (LeetCode 111) | nodes on the shortest root-to-leaf path | 0 | 1 |
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.