implement recursive BFS with queue