basic version
same algo for BFS but reverting list at the end
sol of q107