Challenge - 5 Problems
Level Order BFS Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this level order traversal?
Given the binary tree below, what is the printed output of the level order traversal (BFS)?
DSA Rust
struct Node {
val: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
fn level_order(root: &Option<Box<Node>>) -> Vec<i32> {
let mut result = Vec::new();
let mut queue = std::collections::VecDeque::new();
if let Some(node) = root {
queue.push_back(node);
}
while let Some(current) = queue.pop_front() {
result.push(current.val);
if let Some(left) = ¤t.left {
queue.push_back(left);
}
if let Some(right) = ¤t.right {
queue.push_back(right);
}
}
result
}
fn main() {
let root = Some(Box::new(Node {
val: 1,
left: Some(Box::new(Node {
val: 2,
left: None,
right: None,
})),
right: Some(Box::new(Node {
val: 3,
left: Some(Box::new(Node {
val: 4,
left: None,
right: None,
})),
right: None,
})),
}));
let output = level_order(&root);
println!("{:?}", output);
}Attempts:
2 left
💡 Hint
Remember, level order traversal visits nodes level by level from left to right.
✗ Incorrect
The traversal visits root first (1), then its children left to right (2, 3), then the next level nodes (4). So the output is [1, 2, 3, 4].
🧠 Conceptual
intermediateHow many nodes are visited in level order traversal?
Given a binary tree with 7 nodes, how many nodes will the level order traversal visit?
Attempts:
2 left
💡 Hint
Level order traversal visits every node exactly once.
✗ Incorrect
Level order traversal visits every node in the tree exactly once, so for 7 nodes, it visits all 7.
🔧 Debug
advancedWhat error does this code raise?
What error will this Rust code produce when performing level order traversal?
DSA Rust
fn level_order(root: &Option<Box<Node>>) -> Vec<i32> {
let mut result = Vec::new();
let mut queue = std::collections::VecDeque::new();
if let Some(node) = root {
queue.push_back(node);
}
while let Some(current) = queue.pop_front() {
result.push(current.val);
if let Some(left) = current.left {
queue.push_back(left);
}
if let Some(right) = current.right {
queue.push_back(right);
}
}
result
}Attempts:
2 left
💡 Hint
Check how you access left and right children inside the loop.
✗ Incorrect
The code tries to move out of a borrowed reference when accessing current.left and current.right, which is not allowed in Rust.
🚀 Application
advancedWhich option prints nodes level by level with each level on a new line?
Given a binary tree, which Rust code snippet correctly prints nodes level by level, each level on its own line?
DSA Rust
struct Node {
val: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
// Assume root is defined
Attempts:
2 left
💡 Hint
Use the queue length before processing each level to separate levels.
✗ Incorrect
Option C correctly uses the queue length to process nodes level by level and prints a newline after each level. Others either print newline after every node or miss enqueuing children properly.
🧠 Conceptual
expertWhat is the time complexity of level order traversal on a binary tree with n nodes?
Choose the correct time complexity for performing a level order traversal (BFS) on a binary tree with n nodes.
Attempts:
2 left
💡 Hint
Each node is visited exactly once during traversal.
✗ Incorrect
Level order traversal visits each node once and processes its children, so the time complexity is linear in the number of nodes, O(n).
