-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2263번.swift
29 lines (23 loc) · 877 Bytes
/
2263번.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 출처 : 백준 트리의 순회
// https://www.acmicpc.net/problem/2263
// 풀이 : hogumachu
let n = Int(readLine()!)!
let inOrder = readLine()!.split(separator: " ").map{Int(String($0))!}
let postOrder = readLine()!.split(separator: " ").map{Int(String($0))!}
var result = ""
var inOrderValueToIndex: [Int:Int] = [:]
var visited = Array(repeating: false, count: n)
for i in 0..<n {
inOrderValueToIndex[inOrder[i]] = i
}
visit(0, n-1, 0, n-1)
print(result)
func visit(_ infrom: Int, _ inthrough: Int, _ postfrom: Int, _ postthrough: Int) {
if infrom > inthrough || postfrom > postthrough {
return
}
result += "\(postOrder[postthrough]) "
let next = inOrderValueToIndex[postOrder[postthrough]]!
visit(infrom, next - 1, postfrom, postfrom + next - infrom - 1)
visit(next + 1, inthrough, postfrom + next - infrom, postthrough - 1)
}