Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the "Right Side View of Binary Tree" represent?
easy
A. All nodes at the deepest level of the tree
B. Nodes visible when the tree is viewed from the left side
C. Nodes visible when the tree is viewed from the right side
D. All leaf nodes of the binary tree

Solution

  1. Step 1: Understand the view perspective

    The right side view shows nodes visible when looking at the tree from the right side.
  2. Step 2: Identify visible nodes

    Only the rightmost node at each level is visible from the right side.
  3. Final Answer:

    Nodes visible when the tree is viewed from the right side -> Option C
  4. Quick Check:

    Right side view = rightmost nodes [OK]
Hint: Right side view shows rightmost nodes at each level [OK]
Common Mistakes:
  • Confusing right side view with left side view
  • Thinking it shows all leaf nodes
  • Assuming it shows deepest level nodes only
2. Which TypeScript syntax correctly defines a binary tree node class for this problem?
easy
A. type TreeNode = { val: number; left?: TreeNode; right?: TreeNode }
B. function TreeNode(val) { this.val = val; this.left = null; this.right = null; }
C. interface TreeNode { val: number; left: TreeNode; right: TreeNode; }
D. class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number) { this.val = val; this.left = null; this.right = null; } }

Solution

  1. Step 1: Check class definition syntax

    class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number) { this.val = val; this.left = null; this.right = null; } } uses TypeScript class with proper types and constructor.
  2. Step 2: Compare other options

    function TreeNode(val) { this.val = val; this.left = null; this.right = null; } is JavaScript function, not TypeScript class. interface TreeNode { val: number; left: TreeNode; right: TreeNode; } misses nullability. type TreeNode = { val: number; left?: TreeNode; right?: TreeNode } uses type but lacks constructor.
  3. Final Answer:

    class TreeNode with constructor and nullable children -> Option D
  4. Quick Check:

    TypeScript class with constructor = class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number) { this.val = val; this.left = null; this.right = null; } } [OK]
Hint: Use class with constructor and nullable children for nodes [OK]
Common Mistakes:
  • Using JavaScript function instead of TypeScript class
  • Not allowing null for left/right children
  • Using interface without constructor
3. Given the binary tree below, what is the right side view output?
    1
   / \
  2   3
   \   \
    5   4
function rightSideView(root: TreeNode | null): number[] { const result: number[] = []; if (!root) return result; const queue: (TreeNode | null)[] = [root]; while (queue.length) { 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; }
medium
A. [1, 3, 4]
B. [1, 2, 5]
C. [1, 3, 5]
D. [1, 2, 4]

Solution

  1. Step 1: Traverse level by level

    Level 1: node 1; Level 2: nodes 2, 3; Level 3: nodes 5, 4.
  2. Step 2: Pick rightmost node at each level

    Level 1 rightmost: 1; Level 2 rightmost: 3; Level 3 rightmost: 4.
  3. Final Answer:

    [1, 3, 4] -> Option A
  4. Quick Check:

    Rightmost nodes per level = [1, 3, 4] [OK]
Hint: Pick last node at each level during BFS [OK]
Common Mistakes:
  • Choosing leftmost nodes instead of rightmost
  • Mixing node values from different levels
  • Ignoring null children in traversal
4. Identify the bug in this TypeScript code for right side view and select the fix.
function rightSideView(root: TreeNode | null): number[] {
  const result: number[] = [];
  if (!root) return result;
  const queue: TreeNode[] = [root];
  while (queue.length) {
    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;
}
medium
A. Initialize queue as empty array instead of [root]
B. Change queue type to (TreeNode | undefined)[] and check node is not undefined after shift
C. Remove the if condition checking i === size - 1
D. Push left child after right child in the queue

Solution

  1. Step 1: Analyze queue.shift() return type

    queue.shift() can return undefined if queue is empty, but here size ensures it's not empty. Still, TypeScript requires checking.
  2. Step 2: Fix type safety

    Change queue type to allow undefined and check node is not undefined before accessing properties.
  3. Final Answer:

    Change queue type and check node after shift -> Option B
  4. Quick Check:

    Type safety requires checking shift result [OK]
Hint: Check shift() result for undefined before use [OK]
Common Mistakes:
  • Ignoring possible undefined from shift()
  • Removing correct condition for rightmost node
  • Changing queue initialization incorrectly
5. You want to find the right side view of a binary tree but only have access to a preorder traversal array with nulls for missing nodes, e.g., [1, 2, null, 5, null, null, 3, null, 4]. How can you adapt your approach to get the right side view?
hard
A. Rebuild the tree from preorder with nulls, then perform BFS right side view
B. Directly pick every second element from the preorder array
C. Use preorder array as is and treat it as right side view
D. Reverse the preorder array and pick first elements

Solution

  1. Step 1: Understand preorder with nulls

    The array represents tree nodes with nulls marking missing children, so tree structure is implicit.
  2. Step 2: Rebuild tree then apply right side view

    Reconstruct the binary tree from this array, then run BFS to get right side view nodes.
  3. Final Answer:

    Rebuild tree from preorder with nulls, then BFS -> Option A
  4. Quick Check:

    Need tree structure to find right side view [OK]
Hint: Rebuild tree from traversal before right side view [OK]
Common Mistakes:
  • Assuming preorder array directly gives right side view
  • Ignoring nulls and tree structure
  • Trying to pick nodes without tree reconstruction
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard