0
0
DSA Typescriptprogramming~10 mins

Tree Traversal Level Order BFS 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 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'
Aunshift
Bpop
Cshift
Dpush
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.
2fill in blank
medium

Complete 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'
Ashift
Bunshift
Cpush
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop removes from the end, breaking BFS order.
Using push or unshift does not remove elements.
3fill in blank
hard

Fix 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'
A==
B!=
C!==
D===
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.
4fill in blank
hard

Fill 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'
Apush
Bpop
Cval
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop removes elements instead of adding.
Using incorrect property name like 'val' instead of 'value'.
5fill in blank
hard

Fill 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'
Apush
Bshift
Cvalue
Dpop
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.