Amazon interview question

LC medium BFS question with some follow-ups