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

Open the playground โ†’

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.

Inputroot = [3, 9, 20, null, null, 15, 7]
Output3

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.

Brute force first: Listing every root-to-leaf path and measuring each one works but builds paths you do not need; the recursion gets the same answer in one pass.

Step-by-step approach

  1. If the node is None, return 0.
  2. Recursively get the depth of the left subtree and of the right subtree.
  3. Return 1 + max(left, right).
  4. For very deep trees, the same answer comes from BFS: count how many levels the queue processes.

Python solution

PYTHON 3tested
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

TimeO(n)
SpaceO(h)

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

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.