[LeetCode] 199. Binary Tree Right Side View(Kotlin)
풀이
import java.util.Deque
class Solution {
fun rightSideView(root: TreeNode?): List<Int> {
val rightSide = mutableListOf<Int>()
val q = ArrayDeque<Pair<TreeNode?, Int>>()
q.addLast(Pair(root, 0))
while(!q.isEmpty()){
val curPair = q.first()
val curNode = curPair.first
val curDepth = curPair.second
q.removeFirst()
if(curNode == null) continue
if(rightSide.size <= curDepth) rightSide.add(curNode.`val`)
else rightSide[curDepth] = curNode.`val`
if(curNode.left != null) q.addLast(Pair(curNode.left,curDepth+1))
if(curNode.right != null) q.addLast(Pair(curNode.right,curDepth+1))
}
return rightSide.toList()
}
}