-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1991번.swift
43 lines (37 loc) · 1.33 KB
/
1991번.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// 출처 : 백준 트리 순회
// https://www.acmicpc.net/problem/1991
// 풀이 : hogumachu
import Foundation
func solution() {
let n = Int(readLine()!)!
var table: [[Int]] = Array(repeating: [0, 0], count: n)
var visited: [Bool] = Array(repeating: false, count: n)
var preorderResult = ""
var inorderResult = ""
var postorderResult = ""
for _ in 0..<n {
let value = readLine()!.split(separator: " ").map{Character(String($0))}
value[1] == "." ? nil : (table[Int(value[0].asciiValue!)-65][0] = Int(value[1].asciiValue!)-65)
value[2] == "." ? nil : (table[Int(value[0].asciiValue!)-65][1] = Int(value[2].asciiValue!)-65)
}
func order(_ node: Int) -> Void {
if visited[node] == false {
visited[node] = true
preorderResult += "\(String(UnicodeScalar(UInt8(node + 65))))"
if table[node][0] != 0 {
order(table[node][0])
}
inorderResult += "\(String(UnicodeScalar(UInt8(node + 65))))"
if table[node][1] != 0 {
order(table[node][1])
}
postorderResult += "\(String(UnicodeScalar(UInt8(node + 65))))"
}
}
order(0)
visited = Array(repeating: false, count: n)
print(preorderResult)
print(inorderResult)
print(postorderResult)
}
solution()