-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDay 23 BST Level-Order Traversal.swift
67 lines (52 loc) · 1.44 KB
/
Day 23 BST Level-Order Traversal.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// Day 23 BST Level-Order Traversal.swift
// HackerRank-Solutions/30 Days of Code
//
// Created by Aleksandar Dinic on 25/04/2020.
// Copyright © 2020 Aleksandar Dinic. All rights reserved.
//
/// Source: https://www.hackerrank.com/challenges/30-binary-trees/problem
class Node {
var data: Int
var left: Node?
var right: Node?
init(d: Int) {
data = d
}
}
class Tree {
func insert(root: Node?, data: Int) -> Node? {
if root == nil {
return Node(d: data)
}
if data <= (root?.data)! {
root?.left = insert(root: root?.left, data: data)
} else {
root?.right = insert(root: root?.right, data: data)
}
return root
}
/// Prints the level-order traversal of the binary search tree.
func levelOrder(root: Node?) -> Void {
guard let root = root else { return }
var queue = [Node]()
queue.append(root)
while !queue.isEmpty {
let current = queue.removeFirst()
print(current.data, terminator: " ")
if let leftNode = current.left {
queue.append(leftNode)
}
if let rightNode = current.right {
queue.append(rightNode)
}
}
}
}
var root: Node?
let tree = Tree()
let t = Int(readLine()!)!
for _ in 0..<t {
root = tree.insert(root: root, data: Int(readLine()!)!)
}
tree.levelOrder(root: root)