Leetcode 199 - Binary tree right side view

Note:

  • Don’t used recursion because you don’t know the right most node is a left node or right node.

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.

Example

img

1
2
Input: root = [1,2,3,null,5,null,4]
Output: [1,3,4]
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
/**
* 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[]}
*/
var rightSideView = function(root) {
let result = [];
if (!root) return result;
let queue = [root];
while (queue.length) {
let length = queue.length;
for (let i = 0; i < length; i++) {
let node = queue.shift();
if (i === length-1) result.push(node.val);
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
}
return result;
};