Challenge - 5 Problems
Left Side View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Left Side View for a Simple Binary Tree
What is the output of the following code that prints the left side view of a binary tree?
DSA Javascript
class Node { constructor(val) { this.val = val; this.left = null; this.right = null; } } function leftSideView(root) { const result = []; function dfs(node, level) { if (!node) return; if (level === result.length) { result.push(node.val); } dfs(node.left, level + 1); dfs(node.right, level + 1); } dfs(root, 0); return result; } const root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.right = new Node(5); root.right.right = new Node(4); console.log(leftSideView(root));
Attempts:
2 left
💡 Hint
Think about which nodes are visible when looking from the left side, level by level.
✗ Incorrect
The left side view includes the first node encountered at each level when traversing left to right. At level 0: 1, level 1: 2, level 2: 5.
❓ Predict Output
intermediate2:00remaining
Left Side View Output for a Skewed Tree
What will be the output of the leftSideView function for this skewed binary tree?
DSA Javascript
class Node { constructor(val) { this.val = val; this.left = null; this.right = null; } } function leftSideView(root) { const result = []; function dfs(node, level) { if (!node) return; if (level === result.length) { result.push(node.val); } dfs(node.left, level + 1); dfs(node.right, level + 1); } dfs(root, 0); return result; } const root = new Node(10); root.left = new Node(20); root.left.left = new Node(30); root.left.left.left = new Node(40); console.log(leftSideView(root));
Attempts:
2 left
💡 Hint
Since the tree is skewed to the left, all nodes should be visible from the left side.
✗ Incorrect
Each level has only one node on the left side, so the output includes all nodes in the left chain.
🔧 Debug
advanced2:00remaining
Identify the Bug in Left Side View Implementation
The following code is intended to return the left side view of a binary tree. What is the bug causing incorrect output?
DSA Javascript
function leftSideView(root) {
const result = [];
function dfs(node, level) {
if (!node) return;
dfs(node.left, level + 1);
dfs(node.right, level + 1);
if (level === result.length) {
result.push(node.val);
}
}
dfs(root, 0);
return result;
}Attempts:
2 left
💡 Hint
Think about when nodes are added to the result relative to recursion order.
✗ Incorrect
Adding nodes after recursion causes right nodes to be added before left nodes at the same level, breaking the left side view logic.
🧠 Conceptual
advanced1:30remaining
Understanding Left Side View with BFS
Which statement correctly describes how to get the left side view of a binary tree using breadth-first search (BFS)?
Attempts:
2 left
💡 Hint
Think about which node is visible from the left side at each level.
✗ Incorrect
In BFS, nodes are processed level by level from left to right, so the first node at each level is the leftmost visible node.
🚀 Application
expert2:30remaining
Left Side View with Missing Left Children
Given the binary tree below, what is the left side view output?
Tree structure:
- Root: 7
- Root.right: 9
- Root.right.left: 8
- Root.right.right: 10
Use the standard left side view logic (first node at each level from left to right).
Attempts:
2 left
💡 Hint
Even if left child is missing, the first node at each level from left to right is included.
✗ Incorrect
Level 0: 7, level 1: 9 (only right child), level 2: 8 (left child of 9) is first at that level.