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 Javascript
function rightSideView(root) {
const [1] = [];
// rest of the code
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a queue or stack to store final output instead of an array.
✗ Incorrect
We use 'result' array to store the nodes visible from the right side.
2fill in blank
mediumComplete the code to add the rightmost node's value of each level to the result array.
DSA Javascript
for (let i = 0; i < levelSize; i++) { const node = queue.shift(); if (i === [1] - 1) { result.push(node.val); } // enqueue children }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'queue.length' which changes during iteration.
✗ Incorrect
The rightmost node is at index levelSize - 1 in the current level.
3fill in blank
hardFix the error in the code that enqueues the left and right children of the current node.
DSA Javascript
if (node.[1]) queue.push(node.left); if (node.right) queue.push(node.right);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking 'node.child' or 'node.children' which are invalid.
✗ Incorrect
We must check 'node.left' before pushing it to the queue.
4fill in blank
hardFill both blanks to complete the BFS loop that processes each level of the tree.
DSA Javascript
while (queue.length > 0) { const [1] = queue.length; for (let i = 0; i < [2]; i++) { const node = queue.shift(); // process node } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'queue.length' directly in the loop condition causing errors.
✗ Incorrect
We store the current level size in 'levelSize' and use it in the for loop.
5fill in blank
hardFill all three blanks to complete the function that returns the right side view of a binary tree.
DSA Javascript
function rightSideView(root) {
if (!root) return [];
const result = [];
const queue = [[1]];
while (queue.length > 0) {
const levelSize = queue.length;
for (let i = 0; i < levelSize; i++) {
const node = queue.shift();
if (i === levelSize [2] 1) {
result.push(node.[3]);
}
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
}
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' in index check, pushing node instead of node.val.
✗ Incorrect
We start with the root node in the queue, check if 'i === levelSize - 1' for the rightmost node, and push 'node.val' to result.