All lessons

Depth-First Search (DFS)

9 min · TypeScript examples

Dive deep along one path before backtracking — great for exploring all reachable nodes.

TypeScript · Depth-First Search (DFS)0/5 cleared

Enemy 1 of 5

DFS uses recursion or an explicit stack to go as far as possible, then back up. Mark nodes visited so you don't loop forever.

Counting connected components in a grid is a classic DFS job.

You
Syntax Skeleton

Loading editor...

Ln 1, Col 1Spaces: 4
Sign up to fightTarget output: 3