← Back to TypeScript
Trees and Graphs
A tree is a graph with a parent's discipline, and two traversals — breadth with a queue, depth with a call stack — carry almost every question about them.
Breadth-first by levels
intermediateblankComplete the level-order traversal — the queue that walks a binary tree one row at a time.
Challenge: count the islands
intermediatechallengeCount connected blobs of land in a grid — the depth-first flood fill that sinks each island once.
Challenge: fewest hops across a graph
advancedchallengeFind the length of the shortest path between two nodes of an undirected graph — breadth-first with a visited set.
Review: the traversal that never comes back
intermediatereviewA reachability walk over a dependency graph returns the right set for every acyclic input — and never returns at all once the graph has a cycle. Find what it forgot.