Maximum Depth of Binary Tree - Problem
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.
Input & Output
Example 1 — Balanced Tree
$
Input:
root = [3,9,20,null,null,15,7]
›
Output:
3
💡 Note:
The longest path is from root 3 → 20 → 15 (or 7), which has 3 nodes, so depth is 3.
Example 2 — Linear Tree
$
Input:
root = [1,null,2]
›
Output:
2
💡 Note:
The tree has only a right path: 1 → 2, so the depth is 2.
Example 3 — Single Node
$
Input:
root = [1]
›
Output:
1
💡 Note:
Tree has only one node (the root), so the depth is 1.
Constraints
- The number of nodes in the tree is in the range [0, 104]
- -100 ≤ Node.val ≤ 100
Visualization
Tap to expand
Understanding the Visualization
1
Input Tree
Binary tree with nodes at different levels
2
Find Paths
Identify all root-to-leaf paths
3
Count Levels
Return the maximum number of levels (nodes in longest path)
Key Takeaway
🎯 Key Insight: Tree depth equals 1 + maximum depth of subtrees
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code