Complete the code to add the root node to the queue for level order traversal.
const queue: TreeNode[] = [];
queue.[1](root);We use push to add the root node at the end of the queue.
Complete the code to remove the first node from the queue during BFS.
const currentNode = queue.[1]();We use shift to remove the first element from the queue, following FIFO order.
Fix the error in adding child nodes to the queue only if they exist.
if (currentNode.left [1] null) { queue.push(currentNode.left); }
We check if currentNode.left is not exactly null using !== before adding it.
Fill both blanks to collect node values in level order traversal.
const result: number[] = []; while (queue.length > 0) { const currentNode = queue.shift()!; result.[1](currentNode.[2]); }
We use push to add the current node's value to the result array.
Fill all three blanks to complete the level order BFS function returning node values.
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;
}The root is added with push, nodes removed with shift, and node values accessed via value.
