0
0
DSA Typescriptprogramming~20 mins

Right Side View of Binary Tree in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Right Side View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Right Side View for a Simple Binary Tree
What is the output of the right side view function for the given binary tree?
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[] {
  const result: number[] = [];
  if (!root) return result;
  const queue: (TreeNode | null)[] = [root];
  while (queue.length > 0) {
    const size = queue.length;
    for (let i = 0; i < size; i++) {
      const node = queue.shift()!;
      if (node.left) queue.push(node.left);
      if (node.right) queue.push(node.right);
      if (i === size - 1) result.push(node.val);
    }
  }
  return result;
}

// Tree structure:
//      1
//     / \
//    2   3
//     \   \
//      5   4

const root = new TreeNode(1,
  new TreeNode(2, null, new TreeNode(5)),
  new TreeNode(3, null, new TreeNode(4))
);
console.log(rightSideView(root));
A[1, 3, 5]
B[1, 2, 5]
C[1, 3, 4]
D[1, 2, 4]
Attempts:
2 left
💡 Hint
Think about which nodes are visible when looking from the right side level by level.
🧠 Conceptual
intermediate
1:30remaining
Understanding the Right Side View Concept
Which statement best describes the right side view of a binary tree?
AIt contains all nodes visible when the tree is viewed from the right side, showing the rightmost node at each level.
BIt contains all leaf nodes of the binary tree.
CIt contains all nodes in the tree sorted by their values.
DIt contains the nodes in the leftmost path from root to leaf.
Attempts:
2 left
💡 Hint
Focus on visibility from the right side, not all nodes or sorting.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Right Side View Implementation
What error will this code produce when run, and why?
DSA Typescript
function rightSideView(root: TreeNode | null): number[] {
  const result: number[] = [];
  if (!root) return result;
  const queue: (TreeNode | null)[] = [root];
  while (queue.length > 0) {
    const size = queue.length;
    for (let i = 0; i <= size; i++) {
      const node = queue.shift()!;
      if (node.left) queue.push(node.left);
      if (node.right) queue.push(node.right);
      if (i === size - 1) result.push(node.val);
    }
  }
  return result;
}
ANo error: The function runs correctly and returns the right side view.
BSyntax error: Missing semicolon after queue.shift()!
CWrong output: The function returns an empty array because the loop never runs.
DRuntime error: Cannot read property 'left' of undefined due to accessing queue.shift() when queue is empty.
Attempts:
2 left
💡 Hint
Check the loop condition and how many times queue.shift() is called.
Predict Output
advanced
2:00remaining
Output of Right Side View for a Left-Skewed Tree
What is the output of the right side view function for this left-skewed binary tree?
DSA Typescript
const root = new TreeNode(10,
  new TreeNode(20,
    new TreeNode(30,
      new TreeNode(40), null), null), null
);
console.log(rightSideView(root));
A[10, 20, 30, 40]
B[10]
C[40]
D[10, 30, 40]
Attempts:
2 left
💡 Hint
In a left-skewed tree, the right side view shows all nodes because no right children hide them.
🚀 Application
expert
1:30remaining
Number of Nodes in Right Side View for a Complete Binary Tree
Given a complete binary tree of height 4 (root at height 1), how many nodes will the right side view contain?
A15
B4
C7
D8
Attempts:
2 left
💡 Hint
The right side view contains one node per level.