We use cookies for site analytics. Accept to help us understand how the site is used. See our Privacy Policy for details.
DFS goes as deep as it can before backtracking, which makes it the natural fit for flood-fill, counting connected components, and any "visit everything reachable" question. It is usually shorter to write recursively than BFS is iteratively. Track visited state carefully - the difference between marking on push and marking on pop is a common source of infinite loops.
Connected regions, path existence, or exploring every branch of a structure.
29 problems. Each one runs in an in-browser editor against real test cases, with hints and a worked solution.
The patterns cheat sheet covers all of these in one page - what each one looks like in a problem statement, and the template that follows.