Skip to content

Commit

Permalink
[LeetCode] Add January 29.
Browse files Browse the repository at this point in the history
  • Loading branch information
pin3da authored Jan 29, 2021
1 parent abf304d commit bf93f67
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions solved/LeetCode/Challenges/2020/January/29.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
data class Entry(val x: Int, val y: Int, val value: Int)

val entries = mutableListOf<Entry>()

fun verticalTraversal(root: TreeNode?): List<List<Int>> {
dfs(root, 0, 0)
entries.sortBy { it.x }
return entries.groupBy { it.x }.values
.map{ it.sortedWith(compareBy({-it.y}, {it.value})) }
.map { l -> l.map {it.value} }
}

fun dfs(root: TreeNode?, x: Int, y: Int) {
if (root == null) {
return
}
entries.add(Entry(x, y, root!!.`val`))
dfs(root!!.left, x - 1, y - 1)
dfs(root!!.right, x + 1, y - 1)
}
}

0 comments on commit bf93f67

Please sign in to comment.