Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add the root node to the queue for level order traversal.
DSA Typescript
const queue: TreeNode[] = [];
queue.[1](root); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push removes elements instead of adding.
Using shift or unshift changes the wrong end of the queue.
✗ Incorrect
We use push to add the root node at the end of the queue.
2fill in blank
mediumComplete the code to remove the first node from the queue during BFS.
DSA Typescript
const currentNode = queue.[1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop removes from the end, breaking BFS order.
Using push or unshift does not remove elements.
✗ Incorrect
We use shift to remove the first element from the queue, following FIFO order.
3fill in blank
hardFix the error in adding child nodes to the queue only if they exist.
DSA Typescript
if (currentNode.left [1] null) { queue.push(currentNode.left); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using == or === can cause incorrect checks due to type coercion.
Using != is less strict and may allow undefined values.
✗ Incorrect
We check if currentNode.left is not exactly null using !== before adding it.
4fill in blank
hardFill both blanks to collect node values in level order traversal.
DSA Typescript
const result: number[] = []; while (queue.length > 0) { const currentNode = queue.shift()!; result.[1](currentNode.[2]); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop removes elements instead of adding.
Using incorrect property name like 'val' instead of 'value'.
✗ Incorrect
We use push to add the current node's value to the result array.
5fill in blank
hardFill all three blanks to complete the level order BFS function returning node values.
DSA Typescript
function levelOrder(root: TreeNode | null): number[] {
if (root === null) return [];
const queue: TreeNode[] = [];
const result: number[] = [];
queue.[1](root);
while (queue.length > 0) {
const currentNode = queue.[2]();
result.push(currentNode.[3]);
if (currentNode.left !== null) queue.push(currentNode.left);
if (currentNode.right !== null) queue.push(currentNode.right);
}
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of shift breaks BFS order.
Accessing wrong property name for node value.
Adding root with shift instead of push.
✗ Incorrect
The root is added with push, nodes removed with shift, and node values accessed via value.