Maximum Depth of Binary Tree
Master this topic with zero to advance depth.
Maximum Depth of Binary Tree
Given the root of a binary tree, return its maximum depth.
Examples
Level I: Recursive DFS (Post-order)
Intuition
Max depth is . This naturally fits recursion.
Detailed Dry Run
root(3) -> 1 + max(depth(9), depth(20)). depth(9)=1, depth(20)=2. Result = 3.
Level II: Iterative BFS (Level Order)
Intuition
Traverse the tree level by level. Every completed level increases the depth.
Diagram
Level 1: [3] -> Depth 1
Level 2: [9, 20] -> Depth 2
Level 3: [15, 7] -> Depth 3Detailed Dry Run
Queue: [3]. size=1. Pop 3, push 9,20. depth=1. Queue: [9,20]. size=2. Pop both, push 15,7. depth=2. Final depth=3.
Level III: Iterative DFS (Stack)
Intuition
Manually manage a stack to mimic recursion. Store pairs of (node, depth) to keep track of max depth.
Detailed Dry Run
Stack: [(3,1)]. Pop (3,1), Max=1. Push (20,2), (9,2). Pop (9,2), Max=2. Pop (20,2), Push(7,3), (15,3). Pop (15,3), Max=3.
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.