Day 23: BST Level-Order Traversal

  • + 0 comments

    swift solution

    func levelOrder(root: Node?) -> Void {
            guard let root = root else { return }
            var result: [Int] = []
            var queue:[Node] = []
            queue.append(root)
            while !queue.isEmpty {
                let node = queue.removeFirst()
                if let left = node.left {
                    queue.append(left)
                }
                
                if let right = node.right {
                    queue.append(right)
                }
                result.append(node.data)
            }
            print(result.map { String($0) }.joined(separator: " "))
        	// Complete the function
        } // End of levelOrder function