Level Order Traversal visits nodes level by level from top to bottom and left to right within each level.
Click to reveal answer
beginner
Which data structure is commonly used to implement Level Order Traversal (BFS) in trees?
A queue is used to keep track of nodes to visit next in Level Order Traversal.
Click to reveal answer
beginner
What is the time complexity of Level Order Traversal on a binary tree with n nodes?
The time complexity is O(n) because each node is visited exactly once.
Click to reveal answer
intermediate
In Level Order Traversal, what happens when a node is dequeued?
When a node is dequeued, its value is processed (e.g., printed), and its children are enqueued for later visits.
Click to reveal answer
intermediate
Why is Level Order Traversal also called Breadth-First Search (BFS)?
Because it explores all nodes at the current depth before moving to nodes at the next depth, spreading out like a wave.
Click to reveal answer
What data structure is essential for implementing Level Order Traversal?
AQueue
BStack
CHash Map
DLinked List
✗ Incorrect
A queue is used to process nodes in the order they are discovered, which is key for Level Order Traversal.
In Level Order Traversal, which node is visited first?
ARightmost leaf node
BLeftmost leaf node
CRoot node
DAny random node
✗ Incorrect
Traversal starts at the root node and proceeds level by level.
What is the order of visiting nodes in Level Order Traversal?
ALeft subtree, root, right subtree
BTop to bottom, left to right
CBottom to top, right to left
DRight subtree, root, left subtree
✗ Incorrect
Nodes are visited level by level from top to bottom and within each level from left to right.
What happens to the children of a node during Level Order Traversal?
AThey are added to the queue
BThey are ignored
CThey are processed immediately
DThey are deleted
✗ Incorrect
Children are enqueued to be visited after all nodes at the current level.
What is the time complexity of Level Order Traversal on a tree with n nodes?
AO(1)
BO(log n)
CO(n^2)
DO(n)
✗ Incorrect
Each node is visited once, so the time complexity is linear in the number of nodes.
Explain how Level Order Traversal (BFS) works on a binary tree step-by-step.
Think about visiting nodes like visiting friends floor by floor in a building.
You got /5 concepts.
Describe the difference between Level Order Traversal and Depth First Traversal in trees.
Compare exploring a tree floor by floor versus going deep down one branch first.
You got /5 concepts.
Practice
(1/5)
1. What does Level Order Traversal (BFS) of a binary tree do?
easy
A. Visits nodes in left subtree, then root, then right subtree
B. Visits nodes in right subtree, then root, then left subtree
C. Visits nodes level by level from top to bottom, left to right
D. Visits nodes randomly without any order
Solution
Step 1: Understand Level Order Traversal
Level order traversal visits nodes starting from the root, then all nodes at the next level, and so on, moving left to right within each level.
Step 2: Compare options with definition
Visits nodes level by level from top to bottom, left to right matches this definition exactly, while others describe different traversals or incorrect behavior.
Final Answer:
Visits nodes level by level from top to bottom, left to right -> Option C
Quick Check:
Level Order = Top to bottom, left to right [OK]
Hint: Level order means visit nodes level-wise, left to right [OK]
Common Mistakes:
Confusing level order with inorder or preorder traversal
Thinking BFS visits nodes randomly
Mixing left and right subtree orders
2. Which of the following is the correct way to initialize a queue for BFS in JavaScript?
easy
A. const queue = null
B. const queue = {}
C. const queue = 0
D. const queue = []
Solution
Step 1: Identify data structure for BFS queue
BFS uses a queue to hold nodes to visit. In JavaScript, an array can be used as a queue with push and shift methods.
Step 2: Check options for correct queue initialization
const queue = [] initializes an empty array, suitable for queue operations. Others are incorrect types for queue usage.
Final Answer:
const queue = [] -> Option D
Quick Check:
Queue = array for BFS [OK]
Hint: Use an empty array [] as queue for BFS in JavaScript [OK]
Common Mistakes:
Using object {} instead of array for queue
Initializing queue as null or number
Not knowing how to enqueue/dequeue in JS
3. What is the output of the following code snippet?
class Node {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
function levelOrder(root) {
const result = [];
const queue = [root];
while (queue.length) {
const node = queue.shift();
result.push(node.val);
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
return result;
}
console.log(levelOrder(root));
medium
A. [1, 2, 3, 4, 5]
B. [1, 3, 2, 5, 4]
C. [4, 5, 2, 3, 1]
D. [2, 4, 5, 3, 1]
Solution
Step 1: Trace the BFS traversal on the tree
Start with root (1), then enqueue left (2) and right (3). Next dequeue 2, enqueue its children 4 and 5. Then dequeue 3 (no children). Then dequeue 4 and 5.
Step 2: Collect visited nodes in order
Visited order is 1, 2, 3, 4, 5 as nodes are processed level by level, left to right.
Final Answer:
[1, 2, 3, 4, 5] -> Option A
Quick Check:
Level order output = [1, 2, 3, 4, 5] [OK]
Hint: Follow queue order: root, left child, right child [OK]
Common Mistakes:
Mixing preorder or inorder output with BFS
Forgetting to enqueue children in correct order
Confusing shift() with pop() in queue
4. Find the error in this BFS code snippet for a binary tree:
function bfs(root) {
const queue = [];
queue.push(root);
while (queue.length > 0) {
const node = queue.pop();
console.log(node.val);
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
}
medium
A. Not checking if root is null before pushing
B. Using pop() instead of shift() causes wrong traversal order
C. Missing return statement
D. Using console.log instead of return
Solution
Step 1: Identify queue operation used
The code uses pop() to remove nodes from the queue, which removes from the end (stack behavior).
Step 2: Understand BFS queue behavior
BFS requires removing nodes from the front (shift()), not the end. Using pop() reverses order, causing DFS-like traversal.
Final Answer:
Using pop() instead of shift() causes wrong traversal order -> Option B
Quick Check:
BFS needs shift(), not pop() [OK]
Hint: Use shift() to dequeue in BFS, not pop() [OK]
Common Mistakes:
Confusing pop() with shift() in queue
Ignoring null root check (less critical here)
Expecting return instead of console.log (not error)
5. Given a binary tree, how can you modify level order traversal to return nodes level by level as nested arrays? Example tree:
1
/ \
2 3
/ \
4 5
hard
A. Use a queue and track the number of nodes at each level to group values
B. Use recursion to visit nodes in preorder and group by depth
C. Use a stack to reverse the order of nodes after traversal
D. Traverse left subtree fully before right subtree and collect nodes
Solution
Step 1: Understand grouping nodes by level
To return nodes level by level, we must know how many nodes are at each level during traversal.
Step 2: Use queue size to separate levels
At each iteration, record queue length (nodes at current level), dequeue exactly that many nodes, collect their values, then enqueue their children for next level.
Step 3: Compare options
Use a queue and track the number of nodes at each level to group values describes this approach correctly. Others describe preorder recursion, stack reversal, or incorrect traversal orders.
Final Answer:
Use a queue and track the number of nodes at each level to group values -> Option A