-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDay 22 Binary Search Trees.swift
58 lines (43 loc) · 1.2 KB
/
Day 22 Binary Search Trees.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
//
// Day 22 Binary Search Trees.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-search-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
}
/// Returns the height of the binary search tree.
func getHeight(root: Node?) -> Int {
guard let root = root else { return -1 }
var leftHeight = getHeight(root: root.left)
var rightHeight = getHeight(root: root.right)
return max(leftHeight, rightHeight) + 1
}
}
var root: Node?
let tree = Tree()
let t = Int(readLine()!)!
for _ in 0..<t {
root = tree.insert(root: root, data: Int(readLine()!)!)
}
print(tree.getHeight(root: root))