https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/

func zigzagLevelOrder(_ root: TreeNode?) -> [[Int]] {
    guard let root = root else { return [] }
    var result = [[Int]]()
    var nodes = [root]
    var i = 1
    while !nodes.isEmpty {
        result.append(i % 2 == 1 ? nodes.map{ $0.val } : nodes.map{ $0.val }.reversed())
        nodes = nodes.reduce([], {
            var val = $0
            if let left = $1.left {
                val.append(left)
            }
            if let right = $1.right {
                val.append(right)
            }
            return val
        })
        i += 1
    }
    return result
}

37E484E9-9CF4-4B28-84B0-CA09F08329C9