Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
/* * @Description: 199. Binary Tree Right Side View * @Author: libk * @Github: https://github.com/libk */ /** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } */ /** * @param {TreeNode}root * @return {number[]} */ const rightSideView = function (root) { if (!root) { return [] } const queue = [root] const res = []
while (queue.length) { let curLength = queue.length let cur = null while (curLength--) { cur = queue.shift() if (cur.left) { queue.push(cur.left) } if (cur.right) { queue.push(cur.right) } } res.push(cur.val) } return res }