Easy
Dec 23, 2025#binary-tree
#dfs
104. Maximum Depth of Binary Tree
Given the root of a binary tree, return its maximum depth.
A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Example 1:
Example 2:
- The number of nodes in the tree is in the range [0, 104].
- -100 <= Node.val <= 100
Constraints:
Notes
Intuition
The maximum depth of a binary tree is the longest path from root to any leaf. At each node, the depth is one plus the maximum depth of its subtrees.
Implementation
Create a recursive DFS function that takes the current node and accumulated depth. When the node is None, we've gone past a leaf, so return the current depth. Otherwise, recurse on both children with depth + 1 and return the maximum of the two results.
Edge-cases
An empty tree (null root) returns 0, which the base case handles naturally.
- Time: O(n) — visit every node
- Space: O(n) — worst case recursion depth for a skewed tree; O(log n) for balanced