Coding interview problems โบ Trees & BSTs
๐ณ Trees & BSTs Easy
Maximum Depth of Binary Tree
Maximum depth (height) of a binary tree explained: the one-line recursion, an iterative BFS version, tested Python code, edge cases and a live tree visual.
Also searched as: height of a binary tree ยท find the depth of a tree recursively
Try it live Free
Binary Search Tree & Traversals
Insert values in sorted order in the BST lab and watch the tree grow into a tall chain, then insert a shuffled order and see how much shallower it stays.
Also useful: Recursion & the Call Stack
The problem
Given the root of a binary tree, return its maximum depth: the number of nodes on the longest path from the root down to a leaf. An empty tree has depth 0.
The path 3 -> 20 -> 15 has three nodes.
The key insight
A tree's depth is one more than the deeper of its two subtrees. That sentence is the whole recursion: the base case is an empty tree with depth 0, and every node combines its children's answers. It is the simplest example of a post-order computation, where a node needs its children's results first.
Step-by-step approach
- If the node is
None, return 0. - Recursively get the depth of the left subtree and of the right subtree.
- Return
1 + max(left, right). - For very deep trees, the same answer comes from BFS: count how many levels the queue processes.
Python solution
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val, self.left, self.right = val, left, right
def max_depth(root):
if root is None:
return 0
return 1 + max(max_depth(root.left), max_depth(root.right))Complexity
Every node is visited once. The recursion goes as deep as the tree is tall, so the call stack uses O(h): O(log n) when balanced, O(n) for a chain.
Common mistakes
- Returning 1 for an empty tree, which makes every answer one too large.
- Counting edges instead of nodes; read which one the question asks for, since some versions define depth by edges.
- Ignoring Python's recursion limit (about 1000) on a skewed tree with thousands of nodes; mention the BFS alternative.
Frequently asked questions
What is the time and space complexity of finding the maximum depth?
O(n) time, one visit per node, and O(h) space for the recursion stack where h is the height. In the worst case of a skewed tree h equals n.
How is minimum depth different?
Minimum depth is the shortest root-to-leaf path. The trap is a node with only one child: you must not take min with the missing side's 0. BFS that stops at the first leaf is the cleanest solution.
Is depth the same as height?
For the whole tree, the maximum depth equals the height of the root. Some textbooks count edges rather than nodes, which gives one less, so confirm the definition in the interview.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Trees & BSTs Binary Search Tree & Traversals lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.