DDSA Solutions

Depth-First Search

77 problems · 57 with full explanations

12 Easy40 Medium6 Hard
DFS explores as far as possible before backtracking. Use it for path finding, cycle detection, connected components, and tree traversal. Implement recursively (implicit call stack) or iteratively with an explicit Stack<T>. Mark visited nodes to avoid infinite loops in graphs.

How to practice

To practice Depth-First Search problems effectively, start with the Easy problems listed below, trace through each solution on paper, then re-implement without looking. When you can recognise the depth-first search pattern within 30 seconds of reading a new problem, move on to Medium difficulty. Use the related topic pages and our study guide for a structured progression.

Open the full study guide →

Start here (Easy + explained)

All Depth-First Search problems