-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathleetcode-199-binary-tree-right-side-view.swift
156 lines (111 loc) · 3.83 KB
/
leetcode-199-binary-tree-right-side-view.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
Copyright (c) 2020 David Seek, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
https://leetcode.com/problems/binary-tree-right-side-view/
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
Example:
Input: [1,2,3,null,5,null,4]
Output: [1, 3, 4]
Explanation:
1 <---
/ \
2 3 <---
\ \
5 4 <---
*/
/**
Big O Annotation
Time complexity O(n) where n is the amount of nodes in root.
Space complexity O(n) where n is the amount of nodes in nums.
*/
func rightSideView(_ root: TreeNode?) -> [Int] {
// Without root nothing to see from the side
guard let root = root else {
return []
}
// Get the tree in level order traversal
let levelTraversal = getLevelTraversal(of: root)
// Get a result array of size levels
var result: [Int] = Array(repeating: 0, count: levelTraversal.count)
// Iterate through the traversal
for (level, values) in levelTraversal {
// And get the last element of each level
result[level] = values.last ?? 0
}
return result
}
/**
Simple level order traversal:
Example:
[
0: [1]
1: [4,2]
2: [5,3]
]
*/
func getLevelTraversal(of root: TreeNode) -> [Int: [Int]] {
// The final output dictionary
var levelTraversal: [Int: [Int]] = [:]
// Init a queue
var queue = Queue()
// And enqueue the root at level 0
queue.enqueue(root, at: 0)
// While the queue is not empty
while !queue.isEmpty {
// Dequeue
let (node, level) = queue.dequeue()!
// Check if we already have a mapping for this level
if let mapped = levelTraversal[level] {
// If so, add nodes value
levelTraversal[level] = mapped + [node.val]
} else {
// If not, set it
levelTraversal[level] = [node.val]
}
// Enqueue the left child
if let left = node.left {
queue.enqueue(left, at: level + 1)
}
// And the right child
if let right = node.right {
queue.enqueue(right, at: level + 1)
}
}
return levelTraversal
}
/**
Basic Queue Stack.
All operations are O(1) in time.
*/
struct Queue {
private var enqueueStack: [(node: TreeNode, level: Int)] = []
private var dequeueStack: [(node: TreeNode, level: Int)] = []
init() {}
public var isEmpty: Bool {
return enqueueStack.isEmpty && dequeueStack.isEmpty
}
public mutating func enqueue(_ node: TreeNode, at level: Int) {
enqueueStack.append((node: node, level: level))
}
public mutating func dequeue() -> (node: TreeNode, level: Int)? {
if dequeueStack.isEmpty {
dequeueStack = enqueueStack.reversed()
enqueueStack.removeAll()
}
return dequeueStack.popLast()
}
}