"Here's a secret that'll make the Trees and Graphs tracks feel easy: depth-first and breadth-first search are the same algorithm. The only difference is whether the to-do list is a stack or a queue."
The Frontier
Whenever you explore a structure — a maze, a tree, a network — you keep a frontier: the set of places you've discovered but not yet explored. The algorithm is always the same shape: take a place from the frontier, look at it, add its undiscovered neighbors to the frontier, repeat until the frontier is empty. The entire character of the search comes down to one question: which place do you take next?
The Single Swap That Changes Everything
That "which next?" is answered by the data structure holding the frontier:
- Frontier is a stack → you take the most recently discovered place → you keep plunging down the newest path → depth-first search (DFS). You go as deep as possible before backing up.
- Frontier is a queue → you take the oldest discovered place → you finish everything close before going further → breadth-first search (BFS). You sweep outward in rings, level by level.
That's it. Same code skeleton; swap stack.pop() for queue.popleft() and depth-first becomes breadth-first. Two of the most important algorithms in computing differ by a single line.
Why Each One Matters
BFS explores in order of distance, so it naturally finds the shortest path (in number of steps) — which is why it's the backbone of the Graphs track. DFS dives deep, which suits exhaustively exploring all possibilities, detecting cycles, and topological ordering — and it's exactly the backtracking you'll meet in the Recursion track (where the stack is the call stack itself). The same stack/queue duality you just learned is the engine under both trees and graphs. You've already built the hard part.