solution to depth-first-search