Maximum Depth of Binary Tree

Find the maximum depth of a binary tree.

Pattern

Recursive DFS

This problem follows the Recursive DFS pattern, commonly found in the Trees category. Recognizing this pattern is key to solving it efficiently in an interview setting.

Approach

How to Solve It

Return 1 + max(depth(left), depth(right)). Base case: null returns 0.

Key Insight

The height of a tree is defined recursively — a null tree has height 0, otherwise it's 1 + the taller subtree.

Step-by-step

  1. 1Base case: if root is null, return 0
  2. 2Recursively get the depth of the left subtree
  3. 3Recursively get the depth of the right subtree
  4. 4Return 1 + max(leftDepth, rightDepth)

Pseudocode

def maxDepth(root):
    if not root: return 0
    return 1 + max(maxDepth(root.left), maxDepth(root.right))
Complexity Analysis

Time Complexity

O(n)

Space Complexity

O(h)
More Trees Problems

Master this pattern with YeetCode

Practice Maximum Depth of Binary Tree and similar Trees problems with flashcards. Build pattern recognition through active recall.

Practice this problem