loading

Maximum Depth of Binary Tree — Step-by-Step Visualization

easyLeetCode #104TreeDepth-First SearchBinary Tree

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.

Algorithm Pattern

Recursive Depth-First Search

Key Idea

The maximum depth of a binary tree is 1 plus the maximum of the depths of its left and right subtrees. This can be solved with a simple recursive post-order traversal.

Step-by-Step Approach

  1. Base Case: If the `node` is null, its depth is 0.
  2. Recursive Step: Calculate the depth of the left subtree and the right subtree.
  3. Return: `1 + max(left_depth, right_depth)`.

Common Gotchas

  • Wait, is it depth or height? LeetCode defines depth as the number of nodes from root to leaf, so the root itself has depth 1.

Related Problems