Challenge - 5 Problems
Level Order Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this level order traversal?
Given the binary tree below, what is the printed output of the level order traversal function?
DSA Javascript
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } function levelOrder(root) { if (!root) return []; const queue = [root]; const result = []; while (queue.length > 0) { const node = queue.shift(); result.push(node.value); if (node.left) queue.push(node.left); if (node.right) queue.push(node.right); } return result; } // Tree structure: // 10 // / \ // 5 15 // / \ \ // 3 7 18 const root = new Node(10); root.left = new Node(5); root.right = new Node(15); root.left.left = new Node(3); root.left.right = new Node(7); root.right.right = new Node(18); console.log(levelOrder(root));
Attempts:
2 left
💡 Hint
Think about visiting nodes level by level from left to right.
✗ Incorrect
Level order traversal visits nodes level by level starting from the root, then left to right at each level. So the order is 10 (root), then 5 and 15 (level 2), then 3, 7, and 18 (level 3).
❓ Predict Output
intermediate2:00remaining
What is the output of this level order traversal with null nodes?
What will be the output of the level order traversal function when the tree has some missing children?
DSA Javascript
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } function levelOrder(root) { if (!root) return []; const queue = [root]; const result = []; while (queue.length > 0) { const node = queue.shift(); result.push(node.value); if (node.left) queue.push(node.left); if (node.right) queue.push(node.right); } return result; } // Tree structure: // 1 // / \ // 2 null // / \ // null 3 const root = new Node(1); root.left = new Node(2); root.left.right = new Node(3); console.log(levelOrder(root));
Attempts:
2 left
💡 Hint
Null children are skipped and not added to the queue.
✗ Incorrect
The traversal visits 1 first, then 2 (left child), then 3 (right child of 2). Missing children are ignored.
🧠 Conceptual
advanced1:00remaining
Which data structure is essential for implementing level order traversal?
To perform a level order traversal (BFS) on a binary tree, which data structure is most suitable to keep track of nodes to visit next?
Attempts:
2 left
💡 Hint
Think about visiting nodes in the order they appear level by level.
✗ Incorrect
A queue follows First-In-First-Out order, which matches the level order traversal visiting nodes level by level.
🔧 Debug
advanced1:30remaining
What error does this level order traversal code produce?
Identify the error when running this level order traversal code snippet.
DSA Javascript
function levelOrder(root) {
if (!root) return [];
const queue = [];
const result = [];
queue.push(root);
while (queue.length > 0) {
const node = queue.pop();
result.push(node.value);
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
return result;
}Attempts:
2 left
💡 Hint
Check how nodes are removed from the queue.
✗ Incorrect
Using pop() removes the last element, making the traversal depth-first (stack behavior), reversing the order.
🚀 Application
expert2:00remaining
How many nodes are visited in this level order traversal?
Given the following tree, how many nodes will the level order traversal visit?
DSA Javascript
// Tree structure: // 8 // / \ // 4 12 // / / \ // 2 10 14 // // 11 class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } const root = new Node(8); root.left = new Node(4); root.right = new Node(12); root.left.left = new Node(2); root.right.left = new Node(10); root.right.right = new Node(14); root.right.left.right = new Node(11); function levelOrder(root) { if (!root) return []; const queue = [root]; const result = []; while (queue.length > 0) { const node = queue.shift(); result.push(node.value); if (node.left) queue.push(node.left); if (node.right) queue.push(node.right); } return result; } const visitedNodes = levelOrder(root).length; console.log(visitedNodes);
Attempts:
2 left
💡 Hint
Count all nodes including the root and all children.
✗ Incorrect
The tree has nodes: 8,4,12,2,10,14,11 totaling 7 nodes visited in level order.