BFS and DFS solution