0
0
DSA Javascriptprogramming~20 mins

Right Side View of Binary Tree in DSA Javascript - 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 following JavaScript code that prints the right side view of a binary tree?
DSA Javascript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function rightSideView(root) {
  const result = [];
  if (!root) return result;
  const queue = [root];
  while (queue.length) {
    const levelSize = queue.length;
    for (let i = 0; i < levelSize; i++) {
      const node = queue.shift();
      if (node.left) queue.push(node.left);
      if (node.right) queue.push(node.right);
      if (i === levelSize - 1) result.push(node.val);
    }
  }
  return result;
}

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, 3, 4]
C[1, 2, 5]
D[1, 2, 4]
Attempts:
2 left
💡 Hint
Remember, the right side view shows the last node at each level when viewed from the right side.
🧠 Conceptual
intermediate
1:30remaining
Understanding the Right Side View Concept
Which of the following best describes the right side view of a binary tree?
ANodes at the deepest level of the binary tree only.
BNodes visible when the tree is viewed from the left side, showing the first node at each level.
CAll leaf nodes of the binary tree.
DNodes visible when the tree is viewed from the right side, showing the last node at each level.
Attempts:
2 left
💡 Hint
Think about which nodes you see if you look at the tree from the right side.
Predict Output
advanced
2:00remaining
Output of Right Side View with Unbalanced Tree
What is the output of the following code that prints the right side view of an unbalanced binary tree?
DSA Javascript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function rightSideView(root) {
  const result = [];
  if (!root) return result;
  const queue = [root];
  while (queue.length) {
    const levelSize = queue.length;
    for (let i = 0; i < levelSize; i++) {
      const node = queue.shift();
      if (node.left) queue.push(node.left);
      if (node.right) queue.push(node.right);
      if (i === levelSize - 1) result.push(node.val);
    }
  }
  return result;
}

const root = new TreeNode(10, new TreeNode(5, new TreeNode(3), null), new TreeNode(15, null, new TreeNode(20)));
console.log(rightSideView(root));
A[10, 15, 20]
B[10, 5, 20]
C[10, 15, 3]
D[10, 5, 3]
Attempts:
2 left
💡 Hint
Check the last node at each level from left to right in the queue.
🔧 Debug
advanced
1:30remaining
Identify the Error in Right Side View Implementation
What error will the following code produce when trying to get the right side view of a binary tree?
DSA Javascript
function rightSideView(root) {
  const result = [];
  if (!root) return result;
  const queue = [root];
  while (queue.length) {
    const levelSize = queue.length;
    for (let i = 0; i < levelSize; i++) {
      const node = queue.shift();
      if (node.left) queue.push(node.left);
      if (node.right) queue.push(node.right);
      if (i === levelSize - 1) result.push(node.val);
    }
  }
  return result;
}
ANo error, returns correct output
BSyntaxError: Unexpected token
CTypeError: Cannot read property 'left' of undefined
DReferenceError: queue is not defined
Attempts:
2 left
💡 Hint
Look carefully at the for loop condition and how queue.shift() behaves.
🧠 Conceptual
expert
1:30remaining
Time Complexity of Right Side View Algorithm
What is the time complexity of the standard breadth-first search approach to find the right side view of a binary tree with n nodes?
AO(n), because each node is visited once
BO(log n), because it only visits nodes on the right side
CO(n^2), because it processes each level multiple times
DO(1), because it only collects rightmost nodes
Attempts:
2 left
💡 Hint
Consider how many times each node is processed in the queue.