Maximum Depth of Binary Tree
FreeTreesEasy26 of 75
The problem
Find the number of nodes on the longest path from a binary tree’s root down to a leaf. An empty tree has depth zero.
Example
A root with one leaf child has depth 2.
Need a hint?
A node adds one level above its deepest child.
Write pseudocode, trace the example, or note an edge case. This scratchpad does not run code.
Notes stay in this browser when storage is available.
Read the solution approach
Return zero for null. Otherwise return 1 + max(depth(left), depth(right)). This definition works unchanged for a leaf because both child depths are zero. In a very skewed tree, consider an explicit stack to avoid recursion limits.
Complexity
O(n) time and O(h) stack space.
Before moving on, explain why the algorithm is correct and trace a boundary case without looking at the approach.