Find The Depth of a Binary Tree

How will you find the depth of a binary tree?

2026 Update: Binary Tree Depth — DFS, BFS, and the Diameter Extension

Finding the maximum depth of a binary tree is a foundational tree problem. More interesting: finding the diameter (longest path between any two nodes), which extends the depth insight.

from collections import deque

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

# Max depth — recursive DFS (most elegant)
def max_depth_dfs(root: TreeNode) -> int:
    if not root:
        return 0
    return 1 + max(max_depth_dfs(root.left), max_depth_dfs(root.right))

# Max depth — iterative BFS (level-order)
def max_depth_bfs(root: TreeNode) -> int:
    if not root:
        return 0
    queue = deque([root])
    depth = 0
    while queue:
        depth += 1
        for _ in range(len(queue)):
            node = queue.popleft()
            if node.left:
                queue.append(node.left)
            if node.right:
                queue.append(node.right)
    return depth

# Min depth — different! Must reach a leaf node
def min_depth(root: TreeNode) -> int:
    if not root:
        return 0
    if not root.left:
        return 1 + min_depth(root.right)
    if not root.right:
        return 1 + min_depth(root.left)
    return 1 + min(min_depth(root.left), min_depth(root.right))

# Diameter of binary tree — longest path between any two nodes
def diameter_of_binary_tree(root: TreeNode) -> int:
    """
    The diameter passing through a node = depth(left) + depth(right).
    Track global maximum as we compute depths.
    """
    max_diameter = [0]

    def depth(node):
        if not node:
            return 0
        left_d = depth(node.left)
        right_d = depth(node.right)
        max_diameter[0] = max(max_diameter[0], left_d + right_d)
        return 1 + max(left_d, right_d)

    depth(root)
    return max_diameter[0]

# Build test tree:    1
#                   / 
#                  2   3
#                 / 
#                4   5
root = TreeNode(1,
    TreeNode(2, TreeNode(4), TreeNode(5)),
    TreeNode(3)
)
print(max_depth_dfs(root))       # 3
print(max_depth_bfs(root))       # 3
print(min_depth(root))           # 2 (1→3)
print(diameter_of_binary_tree(root))  # 3 (4→2→1→3 or 5→2→1→3)

Why min_depth is tricky: A node with only one child is NOT a leaf. You must recurse into the existing child, not return 1. This catches many candidates in interviews.

Related problems (LeetCode): #104 (Max Depth), #111 (Min Depth), #543 (Diameter), #124 (Binary Tree Maximum Path Sum — same pattern, track global max). The diameter pattern — compute a local metric and update a global max during DFS — generalizes to many tree problems.

💡Strategies for Solving This Problem

Easier Than It Sounds

This is usually a warm-up question. I've seen it at Google, Microsoft, and smaller companies. Don't overthink it.

What They're Testing

Can you think recursively? That's it. Binary tree problems are all about recursion or level-order traversal.

Approach 1: Recursive (The Standard Answer)

The depth of a tree is: 1 + max(depth of left, depth of right). This screams recursion.

Base case: empty tree has depth 0. Recursive case: 1 + deeper subtree.

This is what 90% of people use and what interviewers expect.

Approach 2: Iterative (BFS)

Use a queue and do level-order traversal. Count how many levels you process. More code, same result.

Only use this if:

  • Interviewer specifically asks for iterative
  • You're worried about stack overflow (very deep trees)
  • You want to show you know BFS

Don't Do This

I once saw someone try to solve this iteratively with a stack (DFS). It works but requires tracking depths manually. Way more complex than needed.

Time Complexity

Both approaches: O(n) where n is number of nodes. You have to visit every node to know the max depth.

Quick tip: If interviewer says "find depth," start coding the recursive solution immediately. It's 5 lines.

Scroll to Top