-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary Tree Vertical Order Traversal.kt
57 lines (46 loc) · 1.48 KB
/
Binary Tree Vertical Order Traversal.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
/**
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
class Solution {
private val cachedList: MutableMap<Int, MutableList<Int>> = mutableMapOf()
private var left = 0
private var right = 0
fun verticalOrder(root: TreeNode?): List<List<Int>> {
if (root == null) {
return emptyList()
}
traverse(root, 0)
return (left..right).map { value ->
cachedList[value].orEmpty()
}
.orEmpty()
}
private fun traverse(root: TreeNode?, column: Int) {
val nodeQueue: Queue<Pair<TreeNode?, Int>> = LinkedList()
nodeQueue.add(root to column)
while (nodeQueue.isNotEmpty()) {
val topNode = nodeQueue.poll()
val column = topNode.second
left = minOf(left, column)
right = maxOf(right, column)
val nodes = cachedList.getOrPut(column) { mutableListOf() }
val safeValue = topNode.first?.`val` ?: 0
nodes.add(safeValue)
val leftNode = topNode?.first?.left
if (leftNode != null) {
nodeQueue.add(leftNode to column - 1)
}
val rightNode = topNode?.first?.right
if (rightNode != null) {
nodeQueue.add(rightNode to column + 1)
}
}
}
}