Maximum Depth of Binary Tree
Detailed guide and Python implementation for the 'Maximum Depth of Binary Tree' problem.
Problem Statement
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.
- •The number of nodes in the tree is in the range [0, 10000]
- •-100 <= Node.val <= 100
Examples
[3,9,20,None,None,15,7]
3
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.
[1,None,2]
2
The tree has root 1 with only a right child 2. Depth is 2.
[]
0
An empty tree has depth 0.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
How to Find the Length of a List in Python
Learn how to find the length of a list in Python using the len() function. Understand the O(1) time complexity and checking counts.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.