Back to Practice Dashboard
Top 150 InterviewHard

Maximum Depth of Binary Tree

Learn how to solve the 'Maximum Depth of Binary Tree' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Hard

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.

The tree is represented as a level-order list where None represents a missing node. Implement a function maxDepth(root: list) -> int.

Constraints
  • The number of nodes in the tree is in the range [0, 10000]
  • -100 <= Node.val <= 100

Examples

Example 1
Input
[3,9,20,None,None,15,7]
Output
3
Explanation

The tree has 3 levels: root [3], second level [9,20], third level [15,7]. The longest path is 3->20->15 or 3->20->7, both of depth 3.

Example 2
Input
[1,None,2]
Output
2
Explanation

The tree has root 1 with only a right child 2. Depth is 2.

Example 3
Input
[]
Output
0
Explanation

An empty tree has depth 0.

Need a Hint?
Perform a recursive tree traversal (DFS) or level-order traversal (BFS) using a queue/stack.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor