Binary Tree Right Side View Visualizer & Step-by-Step Algorithm Solution

Return the values of the nodes you can see ordered from top to bottom when standing on the right side of the binary tree.

Category: trees | Difficulty: Medium

Tags: BFS, Binary Tree, Queue, Right View, Level Order

Binary Tree Right Side View

1
2
5
3
4
BFS QUEUE
FRONT
Node(1)
100%
state
rightView[]
Initialization
1/17
Explanation

Initialize BFS Queue with root Node(1).

Source Code
1function rightSideView(root) {
2 if (!root) return [];
3 const queue = [root];
4 const rightView = [];
5 while (queue.length > 0) {
6 const levelSize = queue.length;
7 for (let i = 0; i < levelSize; i++) {
8 const node = queue.shift();
9 if (i === levelSize - 1) rightView.push(node.val);
10 if (node.left) queue.push(node.left);
11 if (node.right) queue.push(node.right);
12 }
13 }
14 return rightView;
15}