문제 링크 (opens new window)

# 풀이

import Foundation

var ret: [[Int]] = .init(repeating: [], count: 14)
var nodes: [Int] = .init(repeating: 0, count: 1030)
var level = Int(readLine()!)!
nodes = readLine()!.split(separator: " ").map { Int($0)! }

func go(_ start: Int, _ end: Int, _ level: Int) {
    if start >= nodes.count { return }
    if start > end { return }
    if start == end {
        ret[level].append(nodes[start])
        return
    }

    let mid = (start + end) / 2
    ret[level].append(nodes[mid])

    go(start, mid - 1, level + 1)
    go(mid + 1, end, level + 1)
}

go(0, nodes.count, 1)

for nodeList in ret {
    if nodeList.isEmpty { continue }
    for node in nodeList {
        print(node, terminator: " ")
    }
    print()
}