Maximum Depth of Binary Tree
Easy · Trees · Tree · DFS · BFS
Problem
Return the maximum depth of a binary tree — the number of nodes on the longest path from the root down to a leaf.
Examples
Input: root = [3,9,20,None,None,15,7] Output: 3
3 → 20 → 15 is the longest path.
Input: root = [] Output: 0
Constraints
- 0 <= number of nodes <= 10^4
- -100 <= Node.val <= 100
Approaches
- Level-order count — time O(n), space O(w)
Related problems
Start free on Cariara · Coding problems · System design studio