JSFiddle - React, Tailwind, and code Playground
JavaScript
function rightSideView(root) {
const res = []
const q = new Queue()
q.push(root)
while (!q.isEmpty()) {
let rightSide = null
const qLen = q.size()
for (let i = 0; i < qLen; i++) {
const node = q.pop()
if (node) {
rightSide = node
q.push(node.left)
q.push(node.right)
}
}
if (rightSide) {
res.push(rightSide.val)
}
}
return res
}
console.log(rightSideView([1, 2, 3]))