iteration for DFS
sol of q094