Implement iterative DFS.