Challenge - 5 Problems
Level Order BFS Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Level Order Traversal on a Simple Tree
What is the output of the following Java code performing level order traversal (BFS) on the given binary tree?
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; } } public class Main { public static void levelOrder(Node root) { if (root == null) return; java.util.Queue<Node> queue = new java.util.LinkedList<>(); queue.add(root); while (!queue.isEmpty()) { Node current = queue.poll(); System.out.print(current.val + " "); if (current.left != null) queue.add(current.left); if (current.right != null) queue.add(current.right); } } public static void main(String[] args) { Node 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); levelOrder(root); } }
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 (2 and 3), then the next level children (4 and 5). So the output is '1 2 3 4 5 '.
❓ Predict Output
intermediateOutput of Level Order Traversal with Null Children
What will be printed by this Java code performing level order traversal on a binary tree where some nodes have only one child?
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; } } public class Main { public static void levelOrder(Node root) { if (root == null) return; java.util.Queue<Node> queue = new java.util.LinkedList<>(); queue.add(root); while (!queue.isEmpty()) { Node current = queue.poll(); System.out.print(current.val + " "); if (current.left != null) queue.add(current.left); if (current.right != null) queue.add(current.right); } } public static void main(String[] args) { Node root = new Node(10); root.left = new Node(20); root.left.right = new Node(40); root.right = new Node(30); levelOrder(root); } }
Attempts:
2 left
💡 Hint
Check the order nodes are added to the queue: left child first, then right child.
✗ Incorrect
The traversal visits root (10), then left child (20) and right child (30). Then it visits 20's right child (40). So the output is '10 20 30 40 '.
🔧 Debug
advancedIdentify the Error in Level Order Traversal Implementation
What error will this Java code produce when performing level order traversal on a binary tree?
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; } } public class Main { public static void levelOrder(Node root) { java.util.Queue<Node> queue = new java.util.LinkedList<>(); queue.add(root); while (!queue.isEmpty()) { Node current = queue.poll(); System.out.print(current.val + " "); if (current.left != null) queue.add(current.left); if (current.right != null) queue.add(current.right); } } public static void main(String[] args) { Node root = null; levelOrder(root); } }
Attempts:
2 left
💡 Hint
Check what happens when root is null and you add it to the queue.
✗ Incorrect
The code adds root to the queue without checking if root is null. Polling from the queue returns null, then accessing current.val causes NullPointerException.
🚀 Application
advancedNumber of Nodes at Each Level Using Level Order Traversal
Given the following Java code, what is the output showing the count of nodes at each level of the binary tree?
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; } } public class Main { public static void countNodesPerLevel(Node root) { if (root == null) return; java.util.Queue<Node> queue = new java.util.LinkedList<>(); queue.add(root); while (!queue.isEmpty()) { int levelSize = queue.size(); System.out.print(levelSize + " "); for (int i = 0; i < levelSize; i++) { Node current = queue.poll(); if (current.left != null) queue.add(current.left); if (current.right != null) queue.add(current.right); } } } public static void main(String[] args) { Node root = new Node(5); root.left = new Node(3); root.right = new Node(8); root.left.left = new Node(1); root.left.right = new Node(4); root.right.right = new Node(9); countNodesPerLevel(root); } }
Attempts:
2 left
💡 Hint
Count how many nodes are in the queue at each level before processing them.
✗ Incorrect
Level 1 has 1 node (5), level 2 has 2 nodes (3,8), level 3 has 3 nodes (1,4,9). So output is '1 2 3 '.
🧠 Conceptual
expertWhy Use a Queue in Level Order Traversal (BFS)?
Which of the following best explains why a queue is used in level order traversal of a binary tree?
Attempts:
2 left
💡 Hint
Think about how nodes are visited level by level from left to right.
✗ Incorrect
A queue follows First-In-First-Out order, so nodes are processed in the exact order they are discovered, which matches level order traversal requirements.
