Challenge - 5 Problems
Left Side View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Left Side View for a Simple Binary Tree
What is the output of the following Java code that prints the left side view of a binary tree?
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; } } public class Main { static void leftSideView(Node root) { java.util.Queue<Node> queue = new java.util.LinkedList<>(); queue.add(root); while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0; i < size; i++) { Node curr = queue.poll(); if (i == 0) System.out.print(curr.val + " "); if (curr.left != null) queue.add(curr.left); if (curr.right != null) queue.add(curr.right); } } } public static void main(String[] args) { Node root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.right = new Node(4); root.right.right = new Node(5); leftSideView(root); } }
Attempts:
2 left
💡 Hint
The left side view prints the first node at each level from left to right.
✗ Incorrect
The code uses a queue to traverse level by level. It prints the first node at each level, which are 1 (root), 2 (left child of root), and 4 (right child of node 2).
🧠 Conceptual
intermediateUnderstanding Left Side View Node Count
Given a binary tree with 4 levels, what is the maximum number of nodes that can appear in its left side view?
Attempts:
2 left
💡 Hint
The left side view shows one node per level.
✗ Incorrect
The left side view shows exactly one node per level, so for 4 levels, the maximum is 4 nodes.
🔧 Debug
advancedIdentify the Bug in Left Side View Code
What error will the following code produce when run on a non-empty binary tree?
DSA Java
void leftSideView(Node root) {
java.util.Queue<Node> queue = new java.util.LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i <= size; i++) {
Node curr = queue.poll();
if (i == 0) System.out.print(curr.val + " ");
if (curr.left != null) queue.add(curr.left);
if (curr.right != null) queue.add(curr.right);
}
}
}Attempts:
2 left
💡 Hint
Check the for loop condition and how many times poll() is called.
✗ Incorrect
The for loop runs one extra time (i <= size), so poll() returns null on last iteration causing NullPointerException when accessing curr.val.
🚀 Application
advancedLeft Side View of a Skewed Binary Tree
What is the output of the left side view function when run on this skewed binary tree where each node has only a right child: 1 -> 2 -> 3 -> 4?
DSA Java
Node root = new Node(1); root.right = new Node(2); root.right.right = new Node(3); root.right.right.right = new Node(4); leftSideView(root);
Attempts:
2 left
💡 Hint
Even if nodes are only on the right, the left side view prints the first node at each level.
✗ Incorrect
Each level has exactly one node, so the left side view prints all nodes: 1 2 3 4.
❓ Predict Output
expertOutput of Left Side View with Null Root
What is the output of the following code when the root node is null?
DSA Java
public static void leftSideView(Node root) {
if (root == null) {
System.out.print("Empty");
return;
}
java.util.Queue<Node> queue = new java.util.LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
Node curr = queue.poll();
if (i == 0) System.out.print(curr.val + " ");
if (curr.left != null) queue.add(curr.left);
if (curr.right != null) queue.add(curr.right);
}
}
}
public static void main(String[] args) {
Node root = null;
leftSideView(root);
}Attempts:
2 left
💡 Hint
Check the null root condition at the start of the function.
✗ Incorrect
The code checks if root is null and prints 'Empty' before returning, so output is 'Empty'.
