Idea This problem is a simple BFS or DFS. Note: we need to check whether we have visit the key before, otherwise, it will be wrong.