-
Notifications
You must be signed in to change notification settings - Fork 5
/
leetcode-103-binary-tree-zigzag-level-order-traversal.swift
153 lines (117 loc) · 4.22 KB
/
leetcode-103-binary-tree-zigzag-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
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
/**
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-zigzag-level-order-traversal/
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its zigzag level order traversal as:
[
[3],
[20,9],
[15,7]
]
*/
/**
Big O Annotation
Time complexity O(n) where n is the amount of nodes in the BST.
Space complexity O(n) where n is the amount of nodes in the BST.
*/
func zigzagLevelOrder(_ root: TreeNode?) -> [[Int]] {
// If we have no root, return an empty array
guard let root = root else {
return []
}
// Init a queue...
var queue = Queue()
/**
... and append the root node and the level 0
Level 0 because it's a 0 based index top down.
*/
queue.enqueue(root, atLevel: 0)
/**
Init a temporary dictionary
of level and node values
*/
var levels: [Int: [Int]] = [:]
// While we still have nodes in the queue...
while !queue.isEmpty {
// Get the top most node/level tuple
let (node, level) = queue.dequeue()!
// Check if the level already exists in the tree
if let mapped = levels[level] {
// If so, add the value to the array (by combining)
levels[level] = mapped + [node.val]
} else {
// Otherwise add the value as array
levels[level] = [node.val]
}
/**
Add the left node to the queue
and increment the level
*/
if let left = node.left {
queue.enqueue(left, atLevel: level + 1)
}
// Same for the right node
if let right = node.right {
queue.enqueue(right, atLevel: level + 1)
}
}
// Init the output array
var resultArray: [[Int]] = Array(repeating: [], count: levels.count)
// Iterate through the level dictionary
for (key, value) in levels {
/**
Check if the current level is odd
to get the zig zack effect by reversing the values
*/
let isOdd = (key % 2) != 0
resultArray[key] = isOdd ? value.reversed() : value
}
// And done 🙂
return resultArray
}
/**
QueueStack implementation with 2 private stacks.
One for enqueueing and the other for dequeueing.
Time complexity O(1)
This API gives us contant time for enqueue and dequeue.
*/
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, atLevel 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()
}
}