add more elegant recursive and iterative solution based on BFS