forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymmetricTree.kt
213 lines (202 loc) · 5.39 KB
/
SymmetricTree.kt
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package questions
import _utils.UseCommentAsDocumentation
import questions.common.TreeNode
import utils.shouldBe
import java.util.*
/**
* Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
*
* [Source](https://leetcode.com/problems/symmetric-tree/)
*/
@UseCommentAsDocumentation
private fun isSymmetric(root: TreeNode?): Boolean {
if (root == null) return true
val queue: Queue<TreeNodeWithDepth> = LinkedList()
val depthToChildrenMap = TreeMap<Int, MutableList<TreeNode?>>()
queue.add(TreeNodeWithDepth(root, depth = 0))
traverse(queue, depthToChildrenMap)
// start the check from last as intermediate indices have already been checked
val sibilings = depthToChildrenMap.values.reversed()
sibilings.forEach {
// Check the symmetry of the list
if (!isPalindrome(it)) {
return false
}
}
return true
}
private fun traverse(
queue: Queue<TreeNodeWithDepth>,
depthToChildren: TreeMap<Int, MutableList<TreeNode?>>
) {
if (queue.isEmpty()) {
return
}
val node = queue.remove()
val depth = node.depth
if (!depthToChildren.containsKey(depth + 1) && depthToChildren.containsKey(depth)) {
val prevSiblings = depthToChildren[depth]!!
// Check if all the element in the previous depth was null i.e. all of them were leaf elements
// i.e. we are past the last depth
if (prevSiblings.all { it == null }) {
return
} else if (!isPalindrome(prevSiblings)) { // check if previously added siblings are symmetric
return // if it wasn't, break early
}
}
// use the parent depth to get the current depth
// add elements at current depth to the map
depthToChildren[depth + 1] = depthToChildren.getOrDefault(depth + 1, mutableListOf()).apply {
// count the nulls as well
this.add(node?.node?.left)
this.add(node?.node?.right)
}
// dont count the null as it can't be expanded further
// add it to queue as well
if (node?.node?.left != null) {
queue.add(TreeNodeWithDepth(node.node.left, depth + 1))
}
if (node?.node?.right != null) {
queue.add(TreeNodeWithDepth(node?.node?.right, depth + 1))
}
traverse(queue, depthToChildren)
}
private fun isPalindrome(list: List<TreeNode?>): Boolean {
for (i in 0..list.lastIndex) {
if (list[i]?.`val` != list[list.lastIndex - i]?.`val`) {
return false
}
}
return true
}
private data class TreeNodeWithDepth(val node: TreeNode?, val depth: Int)
fun main() {
isSymmetric(
TreeNode.from(
arrayOf(
9,
14,
14,
74,
null,
null,
74,
null,
12,
12,
null,
63,
null,
null,
63,
-8,
null,
null,
-8,
-53,
null,
null,
-53,
null,
-96,
-96,
null,
-65,
null,
null,
-65,
98,
null,
null,
98,
50,
null,
null,
50,
null,
91,
91,
null,
41,
-30,
-30,
41,
null,
86,
null,
-36,
-36,
null,
86,
null,
-78,
null,
9,
null,
null,
9,
null,
-78,
47,
null,
48,
-79,
-79,
48,
null,
47,
-100,
-86,
null,
47,
null,
67,
67,
null,
47,
null,
-86,
-100,
-28,
11,
null,
56,
null,
30,
null,
64,
64,
null,
30,
null,
56,
null,
11,
-28,
43,
54,
null,
-50,
44,
-58,
63,
null,
null,
-43,
-43,
null,
null,
63,
-58,
44,
-50,
null,
54,
43
)
)
) shouldBe true
isSymmetric(TreeNode.from(arrayOf(2, 3, 3, 4, 5, 5, 4, null, null, 8, 9, null, null, 9, 8))) shouldBe false
isSymmetric(TreeNode.from(arrayOf(1, 2, 2, null, 3, null, 3))) shouldBe false
isSymmetric(TreeNode.from(1, 2, 2, 3, 4, 4, 3)) shouldBe true
}