0
0
DSA Javascriptprogramming

Right Side View of Binary Tree in DSA Javascript

Choose your learning style9 modes available
Mental Model
We want to see the nodes that are visible when looking at the tree from the right side, which means the rightmost node at each level.
Analogy: Imagine standing on the right side of a tall building with many floors. You only see the windows on the right edge of each floor, not the ones behind or to the left.
      1
     / \
    2   3
     \   \
      5   4

Right side view: 1 -> 3 -> 4
Dry Run Walkthrough
Input: Binary tree: 1 -> 2, 3; 2 -> null, 5; 3 -> null, 4
Goal: Find the rightmost node at each level to form the right side view list
Step 1: Start at root node 1, add it to queue for level order traversal
Queue: [1]
Why: We begin level by level from the root to find rightmost nodes
Step 2: Process level 0: dequeue 1, enqueue children 2 and 3; record 1 as rightmost at this level
Queue: [2, 3]
Right side view: [1]
Why: Node 1 is the only node at level 0, so it is the rightmost
Step 3: Process level 1: dequeue 2, enqueue child 5; dequeue 3, enqueue child 4; record 3 as rightmost at this level
Queue: [5, 4]
Right side view: [1, 3]
Why: At level 1, nodes are 2 and 3; 3 is the rightmost
Step 4: Process level 2: dequeue 5 (no children), dequeue 4 (no children); record 4 as rightmost at this level
Queue: []
Right side view: [1, 3, 4]
Why: At level 2, nodes are 5 and 4; 4 is the rightmost
Result:
Right side view list: [1, 3, 4]
Annotated Code
DSA Javascript
class TreeNode {
  constructor(val = 0, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function rightSideView(root) {
  if (!root) return [];
  const result = [];
  const queue = [root];

  while (queue.length > 0) {
    const levelSize = queue.length;
    for (let i = 0; i < levelSize; i++) {
      const node = queue.shift();
      // If this is the last node in the current level, add its value
      if (i === levelSize - 1) {
        result.push(node.val);
      }
      if (node.left) queue.push(node.left);
      if (node.right) queue.push(node.right);
    }
  }
  return result;
}

// Driver code
const root = new TreeNode(1,
  new TreeNode(2, null, new TreeNode(5)),
  new TreeNode(3, null, new TreeNode(4))
);
const view = rightSideView(root);
console.log(view.join(', '));
if (!root) return [];
handle empty tree edge case
const queue = [root];
initialize queue for level order traversal starting at root
const levelSize = queue.length;
determine number of nodes at current level
const node = queue.shift();
remove node from front of queue to process
if (i === levelSize - 1) { result.push(node.val); }
add value of rightmost node at this level to result
if (node.left) queue.push(node.left);
enqueue left child if exists
if (node.right) queue.push(node.right);
enqueue right child if exists
OutputSuccess
1, 3, 4
Complexity Analysis
Time: O(n) because we visit each node exactly once during the level order traversal
Space: O(n) because in the worst case the queue can hold all nodes at the last level
vs Alternative: Compared to a DFS approach, BFS naturally processes nodes level by level, making it easier to identify rightmost nodes without extra bookkeeping
Edge Cases
Empty tree (root is null)
Returns an empty list since there are no nodes to view
DSA Javascript
if (!root) return [];
Tree with only one node
Returns a list with that single node's value
DSA Javascript
const queue = [root];
Tree where all nodes only have left children
Returns all nodes since each level has only one node which is also the rightmost
DSA Javascript
if (node.left) queue.push(node.left);
When to Use This Pattern
When asked to find nodes visible from one side of a tree, use level order traversal and pick the last node at each level to get the right side view.
Common Mistakes
Mistake: Adding the first node of each level instead of the last node
Fix: Change condition to add node value only when processing the last node in the level (i === levelSize - 1)
Mistake: Using preorder or inorder traversal which does not process nodes level by level
Fix: Use BFS with a queue to process nodes level by level
Summary
Finds the rightmost node at each level of a binary tree to show the right side view.
Use when you need to see the tree as if looking from the right side, level by level.
The key is to do a level order traversal and record the last node visited at each level.