Practice question · Multiple choice
Depth-first search can be written recursively with no visible stack, or iteratively with an explicit one. What is the recursive version actually using?
Hints
- Ask what a stack frame stores, and what DFS needs to remember at each branch.
- Both versions can overflow. Ask what runs out in each case.
Show the answer
A. The call stack, borrowed to remember where to resume
Why
Each frame holds the node you must come back to, which is exactly what the explicit stack holds, same LIFO discipline, different owner. Recursion is more readable and bounded by stack depth; the explicit version can be given a heap-allocated stack, which is how graph traversal survives a million-node input.
Practise Stacks and Queues
The app has 7 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.