0
0
DSA Typescriptprogramming

Right Side View of Binary Tree in DSA Typescript

Choose your learning style9 modes available
Mental Model
We want to see the nodes 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 and windows. 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: root=1, left=2, right=3, 2's right=5, 3's right=4
Goal: Find the list of nodes visible from the right side: one node per level, the rightmost one
Step 1: Start at root (level 0), add 1 to result
Level 0: [1]
Result: [1]
Why: Root is always visible from the right side
Step 2: Move to level 1, nodes are 2 and 3; pick rightmost 3
Level 1: [2, 3]
Result: [1, 3]
Why: At each level, only the rightmost node is visible
Step 3: Move to level 2, nodes are 5 (from 2) and 4 (from 3); pick rightmost 4
Level 2: [5, 4]
Result: [1, 3, 4]
Why: Rightmost node at level 2 is 4, so it is visible
Step 4: No more levels, end traversal
Traversal complete
Result: [1, 3, 4]
Why: All levels processed, final right side view collected
Result:
Right side view nodes: [1, 3, 4]
Annotated Code
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function rightSideView(root: TreeNode | null): number[] {
  if (!root) return [];
  const result: number[] = [];
  const queue: (TreeNode | null)[] = [root];

  while (queue.length > 0) {
    const levelSize = queue.length;
    for (let i = 0; i < levelSize; i++) {
      const node = queue.shift()!;
      // Add children for next level
      if (node.left) queue.push(node.left);
      if (node.right) queue.push(node.right);
      // If last node in this level, add to result
      if (i === levelSize - 1) {
        result.push(node.val);
      }
    }
  }
  return result;
}

// Driver code using dry_run input
const root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.right = new TreeNode(5);
root.right.right = new TreeNode(4);

const view = rightSideView(root);
console.log(view.join(", ") + "\n");
if (!root) return [];
handle empty tree edge case
const queue: (TreeNode | null)[] = [root];
initialize queue with root for level order traversal
const levelSize = queue.length;
determine number of nodes at current level
const node = queue.shift()!;
remove node from front of queue to process
if (node.left) queue.push(node.left);
enqueue left child for next level
if (node.right) queue.push(node.right);
enqueue right child for next level
if (i === levelSize - 1) { result.push(node.val); }
add rightmost node of current level to result
OutputSuccess
1, 3, 4
Complexity Analysis
Time: O(n) because we visit each node exactly once during level order traversal
Space: O(n) because the queue can hold up to all nodes at the widest level
vs Alternative: Compared to DFS with tracking depth, BFS is straightforward and naturally processes nodes level by level, making it easier to pick rightmost nodes
Edge Cases
Empty tree (root is null)
Returns empty list immediately
DSA Typescript
if (!root) return [];
Tree with only one node
Returns list with that single node value
DSA Typescript
const queue: (TreeNode | null)[] = [root];
Tree where some nodes have only left children
Still returns rightmost nodes per level, which may be left children if no right child exists
DSA Typescript
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: Add the node only when processing the last node in the current level (i === levelSize - 1)
Mistake: Using preorder or inorder traversal which does not process nodes level by level
Fix: Use BFS (level order traversal) 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 which nodes are visible from the right side of a tree.
The key insight is to traverse level by level and pick the last node at each level.