0
0
DSA Typescriptprogramming~10 mins

Right Side View of Binary Tree in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the result array for storing the right side view nodes.

DSA Typescript
const rightSideView = (root: TreeNode | null): number[] => {
  const result: number[] = [1];
  if (!root) return result;
  // further code
};
Drag options to blanks, or click blank then click option'
Anull
B{}
C[]
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using {} which is an object, not an array.
Using null or 0 which are not arrays.
2fill in blank
medium

Complete the code to perform a breadth-first search using a queue initialized with the root node.

DSA Typescript
const queue: (TreeNode | null)[] = [1];
while (queue.length > 0) {
  // processing nodes
}
Drag options to blanks, or click blank then click option'
A[null]
Broot
C[]
D[root]
Attempts:
3 left
💡 Hint
Common Mistakes
Using root without array brackets causes errors.
Starting with empty array means no nodes to process.
3fill in blank
hard

Fix the error in the code to add the rightmost node's value of the current level to the result array.

DSA Typescript
for (let i = 0; i < levelSize; i++) {
  const node = queue.shift()!;
  if (i === [1]) {
    result.push(node.val);
  }
  // enqueue children
}
Drag options to blanks, or click blank then click option'
AlevelSize - 1
B0
ClevelSize
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 adds the leftmost node instead.
Using levelSize causes out-of-bound index.
4fill in blank
hard

Fill both blanks to enqueue the left and right children of the current node if they exist.

DSA Typescript
if (node.[1]) queue.push(node.[1]);
if (node.[2]) queue.push(node.[2]);
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cval
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'val' or 'parent' which are not child nodes.
Swapping left and right properties.
5fill in blank
hard

Fill all three blanks to complete the function that returns the right side view of the binary tree.

DSA Typescript
const rightSideView = (root: TreeNode | null): number[] => {
  const result: number[] = [1];
  if (!root) return result;
  const queue: (TreeNode | null)[] = [2];
  while (queue.length > 0) {
    const levelSize = queue.length;
    for (let i = 0; i < levelSize; i++) {
      const node = queue.shift()!;
      if (i === levelSize - 1) {
        result.push(node.val);
      }
      if (node.left) queue.push(node.left);
      if (node.right) queue.push(node.right);
    }
  }
  return [3];
};
Drag options to blanks, or click blank then click option'
A[]
B[root]
Cresult
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Returning null or queue instead of result.
Not initializing queue with root node.