class Solution { var res = [Int]() func levelOrder(_ root: TreeNode?) -> [Int] { var queue = [TreeNode]() if root == nil { return res } queue.append(root!) bfs(root,&queue) return res }
func bfs(_ head: TreeNode?, _ arr: inout Array<TreeNode>) { while (arr.count > 0) { let node = arr[0] if let left = node.left { arr.append(left) } if let right = node.right { arr.append(right) } res.append(node.val) arr.remove(at:0) } } }