← Visualizers
Graphs · Flood fill on a grid LeetCode 695

Max Area of Island / visualized

Problem 200 counts components; this one measures them. The scan, the four neighbours and the sink-before-you-recurse rule are unchanged — the single edit is the return type. void becomes int, and every call owes its caller 1 + down + up + right + left. The sum is assembled on the way down and collapses on the way back up; watch the returns land.

Execution

idle
INVARIANT The DFS returns an integer: 1 + ∑(neighbour returns). Sinking in-place guarantees each cell contributes to exactly one island.
Call Stack · Return Value Accumulator Top = Active · Bottom = Suspended Parents
Press Run to begin.
0 / 0
Speed

Java · running line

scan cursor call on top of stack this island, returned island finished water
INTERVIEW SCRIPT

How to Explain this Accumulator Recursion to an Interviewer

1. The Accumulator Contract

"Unlike LC 200's void sink, area(r, c) returns an int representing the total area of the island component rooted at (r, c)."

2. Post-Order Bubbling (The Equation)

"The sum collapses in post-order: 1 (self) + down + up + right + left. Child branches evaluate first; when they return, the parent adds their results."

3. Frame Suspension with State

"While area(r, c) waits on Down, it sits suspended on the call stack holding 1. When Down returns 2, the parent records it and moves to Up."

4. Base Cases & Space Complexity

"Off-grid and water return 0 immediately without allocating new frames. Worst-case space is O(m · n) call stack depth for a continuous spiral island."

O(m·n) time · O(m·n) worst-case recursion depth  ·  the sum is assembled on the stack: 1 + sum(children). Sinking is what guarantees each cell is counted once and only once.