Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using {} which is an object, not an array.
Using null or 0 which are not arrays.
✗ Incorrect
We initialize result as an empty array [] to store the right side view nodes.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using root without array brackets causes errors.
Starting with empty array means no nodes to process.
✗ Incorrect
We start the queue with the root node inside an array: [root].
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 adds the leftmost node instead.
Using levelSize causes out-of-bound index.
✗ Incorrect
The rightmost node is at index levelSize - 1 in the current level.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'val' or 'parent' which are not child nodes.
Swapping left and right properties.
✗ Incorrect
We check and enqueue node.left and node.right children.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning null or queue instead of result.
Not initializing queue with root node.
✗ Incorrect
Initialize result as [], queue as [root], and return result at the end.