use dfs and bfs to rewrite qeustions