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)?
Tree structure:
1
/ \
2 3
/ / \
4 5 6
Tree structure:
1
/ \
2 3
/ / \
4 5 6
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def level_order(root): if not root: return [] queue = [root] result = [] while queue: node = queue.pop(0) result.append(node.val) if node.left: queue.append(node.left) if node.right: queue.append(node.right) print(' -> '.join(map(str, result)) + ' -> null') root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.right.left = Node(5) root.right.right = Node(6) level_order(root)
Attempts:
2 left
💡 Hint
Remember, level order traversal visits nodes level by level from left to right.
✗ Incorrect
Level order traversal visits nodes starting from the root, then all nodes at the next level from left to right, and so on. So the order is 1, then 2 and 3, then 4, 5, and 6.
❓ Predict Output
intermediateOutput of level order traversal with missing children
What is the output of the level order traversal for this tree?
Tree structure:
10
/ \
20 30
\ \
40 50
Tree structure:
10
/ \
20 30
\ \
40 50
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def level_order(root): if not root: return [] queue = [root] result = [] while queue: node = queue.pop(0) result.append(node.val) if node.left: queue.append(node.left) if node.right: queue.append(node.right) print(' -> '.join(map(str, result)) + ' -> null') root = Node(10) root.left = Node(20) root.right = Node(30) root.left.right = Node(40) root.right.right = Node(50) level_order(root)
Attempts:
2 left
💡 Hint
Check the order nodes are added to the queue: left child first, then right child.
✗ Incorrect
Level order visits root first (10), then its children left to right (20, 30), then their children (40 from 20's right, 50 from 30's right).
🔧 Debug
advancedIdentify the error in this level order traversal code
What error does this code raise when run on a non-empty tree?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def level_order(root): if root is None: return [] queue = [] result = [] queue.append(root) while queue: node = queue.pop() result.append(node.val) if node.left: queue.append(node.left) if node.right: queue.append(node.right) print(' -> '.join(map(str, result)) + ' -> null')
Attempts:
2 left
💡 Hint
Check how elements are removed from the queue.
✗ Incorrect
Using pop() without index removes the last element, making the traversal depth-first (stack behavior), reversing the order of nodes visited compared to BFS.
🧠 Conceptual
advancedWhat is the time complexity of level order traversal?
Given a binary tree with n nodes, what is the time complexity of performing a level order traversal?
Attempts:
1 left
💡 Hint
Each node is visited exactly once.
✗ Incorrect
Level order traversal visits each node once and processes it in constant time, so total time is proportional to number of nodes, O(n).
🚀 Application
expertFind the maximum value at each level using level order traversal
Given a binary tree, which code snippet correctly prints the maximum value at each level using level order traversal?
Attempts:
2 left
💡 Hint
Remember to process all nodes at the current level before printing max.
✗ Incorrect
Option D correctly processes nodes level by level using a for loop over level_size, updates max_val per level, and prints after each level. Others either do not separate levels or pop from wrong end.
