Problem LeetCode 200. Number of Islands ↗ · NeetCode ↗· The solutions on this page are my own.

Number of Islands Visualizer

Four solutions run step by step next to their Java code. Click any cell to turn it into land or water, and every solution reruns on your map.

The problem, step by step

Skip to the visualizer ↓

Count the islands on a map of land and water

You get a grid where every cell is land or water. Land cells that touch each other side by side form one island. Count the islands.

  1. What you get

    A grid of characters with m rows and n columns: '1' for land and '0' for water.

  2. What you return

    One number: how many separate islands the grid has.

  3. The rules

    • Two land cells are on the same island when they share a side: up, down, left or right.
    • Touching only at a corner (diagonally) does not connect them.
    • Everything outside the grid counts as water, so an island can sit right against the edge.
    • An island can have any shape and any size, even a single cell.
  4. Try it by hand

    Input
    the 4 by 5 map below
    Output
    4
    the map
    its 4 islands
    1. Scan the grid row by row, left to right. The first land cell is at row 0, column 0. It belongs to no island yet, so it starts a new one: A.
    2. Spread from it to every land cell that shares a side: (0,1) to the right and (1,0) below. Both are A, and nothing else touches them.
    3. Keep scanning. (0,4) is land and not part of any island yet: island B. It spreads down to (1,4) and from there left to (1,3).
    4. (2,2) is land. It touches (1,3) and (3,1) only at the corners, so it is an island of its own: C.
    5. (3,1) is the last land cell that has no island yet: D.
    6. Four islands were started, so the answer is 4.
  5. Edge cases to check

    • Diagonal neighbors stay apart: a 2 by 2 map with land only at (0,0) and (1,1) has 2 islands.
    • All water gives 0. All land gives 1, however big the grid is, and on a large grid that is also the case that makes a recursive solution go very deep.
    • The cells are characters, so compare with '1', not the number 1.
    • Count every cell once: a land cell that already belongs to an island must not start a new one.
  6. The question behind the solutions

    Finding a land cell is easy. The real work is marking every cell of its island, so the scan never counts the same island twice. How do you walk an island of any shape and visit each of its cells exactly once?

How to walk an island

The 4 by 5 map from above, with its islands A to D in scan order, sketched the way you would draw it in an interview. Every approach scans the grid the same way; they differ in how they mark the rest of an island. Then step through all four solutions below; the queue and the stack share one board here, and the recursive function, traverseIsland in the code, is called sink in the drawing.

1. Recursive DFS traverseIsland

Remembers: nothing extra; sunk cells become water, and the call stack does the walking.

A1A3B1A2B3B2C1D1012340123call stack for BnumIslandssink(0,4)sink(1,4)sink(1,3)deepest: 3 callsthe scan starts A, B, C and D: count = 4all land, 1000 × 1000: a million calls deep
  1. Scan row by row. A land cell starts a new island: add 1 to the count.
  2. Sink it (set it to '0') and call the same function on its four neighbors.
  3. A call on water or off the grid returns at once, so every cell is sunk exactly once.

The catch: the call stack can grow as big as the island. One big island of a million cells overflows it.

2. BFS queue or DFS stack NumberOfIslandsBFS, NumberOfIslandsDFSStack

Remembers: the land cells found but not yet expanded, in a queue or a stack you own.

11111111012340123queue (BFS)stack (DFS)0,00,01,00,11,00,10,11,0emptyemptytakes the oldesttakes the newestqueue: spreads out in rings from the startstack: runs as deep as it can firstboth sink a cell when it is added, so no cell goes in twice
  1. A new island: sink the cell and add it.
  2. Take a cell, sink each land neighbor and add it.
  3. When the queue or stack is empty, the island is done; carry on scanning.

The catch: same O(R × C) time, and the extra memory still grows with the island, but on the heap, so no stack overflow.

3. Union-Find NumberOfIslandsUnionFind

Remembers: a parent for every cell, plus a rank, and a count.

012340123corners do not joineach land cell is its own setcount = 8union with the cell to theright and the cell below4 merges8 − 4 = 4 islands ✓the grid itself is never changed
  1. Every land cell starts as its own set; count them.
  2. For each land cell, union it with land to the right and below.
  3. Each union of two different sets takes 1 off the count.

The catch: more code and O(R × C) memory for the parents. It pays off when land arrives cell by cell and you need the count after each one.

Four ways to solve it, step by step

Algorithm
Map

0islands found
0call depth
0deepest so far

Click a cell to switch it between land and water.

Call stack

water '0' land '1' sunk, colored by island waiting current cell

lines that run are highlighted

      

Complexity · grid of m rows × n columns

time
extra space, worst case

All four compared

AlgorithmTimeExtra spaceStored onChanges grid

Related in the Lab BFS and DFS, the two searches used here · Word Search, another grid walk

© 2026 Arman Ajabkhani · armantik.com · Privacy