Back to problems Sign in to unlock
Leetcode
Easy
Trees
Recursion
Queues
Maximum Depth Of Binary Tree
Find the number of nodes on the longest root-to-leaf path in a binary tree.
Acceptance 100%
Problem Statement
Given the root of a binary tree, determine its maximum depth. The maximum depth is the number of nodes along the longest path from the root down to any leaf node.
A leaf is a node with no children.
Input Format
- A binary tree root node.
- Each node contains an integer value and references to left and right children.
Output Format
- Return a single integer representing the maximum depth of the tree.
Constraints
- The tree may be empty.
- The tree can be highly unbalanced.
- Solve efficiently using tree traversal.
Examples
Sample cases returned by the problem API.
Example 1
Input
root = [3,9,20,null,null,15,7]
Output
3
Explanation
The longest path is 3 -> 20 -> 15 (or 3 -> 20 -> 7), which has 3 nodes.
Example 2
Input
root = [1,null,2]
Output
2
Explanation
The longest path is 1 -> 2, so the maximum depth is 2.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.