📖 Scenario: Imagine you have a family tree represented as a binary tree. You want to see only the leftmost family members at each generation level.
🎯 Goal: Build a program that finds the left side view of a binary tree, showing the first node visible at each level from the left side.
📋 What You'll Learn
Create a binary tree using nodes with val, left, and right properties
Use a variable to keep track of the current level during traversal
Implement a function to collect the leftmost nodes at each level
Print the array of left side view node values
💡 Why This Matters
🌍 Real World
Left side view of a binary tree helps in visualizing hierarchical data from a specific perspective, useful in family trees, organizational charts, and decision trees.
💼 Career
Understanding tree traversal and views is important for software engineers working with hierarchical data structures, UI rendering, and algorithms.
Progress0 / 4 steps
1
Create the Binary Tree
Create a binary tree with a root node called root having value 1. Its left child should have value 2 and right child should have value 3. The left child of node 2 should have value 4, and the right child of node 3 should have value 5.
DSA Javascript
Hint
Use objects with val, left, and right properties to build the tree.
2
Set Up Level Tracking Variable
Create an empty array called leftView to store the left side view node values.
DSA Javascript
Hint
Use const leftView = []; to create an empty array.
3
Implement Left Side View Function
Write a function called leftSideView that takes node and level as parameters. If node is null, return immediately. If level is equal to the length of leftView, push node.val into leftView. Then recursively call leftSideView for the left child with level + 1, followed by the right child with level + 1. Finally, call leftSideView with root and 0.
DSA Javascript
Hint
Use recursion to visit nodes level by level, adding the first node at each level to leftView.
4
Print the Left Side View
Print the leftView array using console.log(leftView).
DSA Javascript
Hint
Use console.log(leftView) to see the left side view array.
Practice
(1/5)
1. What does the "Left Side View" of a binary tree represent?
easy
A. All leaf nodes of the tree
B. Nodes visible when the tree is viewed from the right side
C. Nodes visible when the tree is viewed from the left side
D. Nodes at the deepest level of the tree
Solution
Step 1: Understand the view perspective
The left side view shows nodes visible when looking at the tree from the left side.
Step 2: Identify visible nodes
Only the first node at each level from the left is visible in this view.
Final Answer:
Nodes visible when the tree is viewed from the left side -> Option C
Quick Check:
Left side view = nodes visible from left [OK]
Hint: Think of standing on the left side and seeing nodes [OK]
Common Mistakes:
Confusing left side view with right side view
Including all leaf nodes instead of first nodes per level
Thinking it shows deepest nodes only
2. Which JavaScript data structure is best suited to help implement the left side view of a binary tree using level order traversal?
easy
A. Queue
B. Stack
C. Set
D. Map
Solution
Step 1: Identify traversal method
Level order traversal visits nodes level by level, left to right.
Step 2: Choose data structure for level order
A queue is used to process nodes in FIFO order, perfect for level order traversal.
Final Answer:
Queue -> Option A
Quick Check:
Level order traversal uses queue [OK]
Hint: Use queue for breadth-first level traversal [OK]
Common Mistakes:
Using stack which is LIFO, not suitable for level order
Choosing set or map which don't maintain order
Confusing queue with stack
3. What is the output of the following code that prints the left side view of the binary tree?
class Node {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
function leftSideView(root) {
if (!root) return [];
const result = [];
const queue = [root];
while (queue.length) {
const levelSize = queue.length;
for (let i = 0; i < levelSize; i++) {
const node = queue.shift();
if (i === 0) result.push(node.val);
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
}
return result;
}
const 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);
root.right.right = new Node(6);
console.log(leftSideView(root));
medium
A. [1, 2, 5]
B. [1, 2, 4]
C. [1, 3, 6]
D. [1, 4, 6]
Solution
Step 1: Traverse level by level and pick first node
Hint: Pick first node at each level during BFS [OK]
Common Mistakes:
Picking right child instead of left
Including all nodes instead of first per level
Confusing order of nodes
4. The following code attempts to print the left side view of a binary tree but produces incorrect output. What is the main bug?
function leftSideView(root) {
if (!root) return [];
const result = [];
const queue = [root];
while (queue.length) {
const levelSize = queue.length;
for (let i = 0; i <= levelSize; i++) {
const node = queue.shift();
if (i === 0) result.push(node.val);
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
}
return result;
}
medium
A. The for loop condition should be i < levelSize, not i <= levelSize
B. The queue should be a stack for correct traversal
C. The check for i === 0 should be i === levelSize - 1
D. The function should return result.length instead of result
Solution
Step 1: Analyze the for loop condition
The loop runs from i = 0 to i <= levelSize, which means one extra iteration causing shift() on empty queue.
Step 2: Correct the loop condition
It should be i < levelSize to process exactly levelSize nodes per level.
Final Answer:
The for loop condition should be i < levelSize, not i <= levelSize -> Option A
Quick Check:
Loop must run exactly levelSize times [OK]
Hint: Use < not <= for loop over fixed size [OK]
Common Mistakes:
Using <= causes extra iteration and errors
Confusing queue with stack for BFS
Wrong index check for first node
5. Given a binary tree where some nodes only have right children, how would the left side view output look for this tree?
1
\
2
\
3
\
4
hard
A. []
B. [1]
C. [4]
D. [1, 2, 3, 4]
Solution
Step 1: Understand left side view with only right children
Each level has only one node, which is the right child, but also the first node at that level.
Step 2: Collect nodes visible from left side
Since no left children, the right child nodes are visible from left side as they are the only nodes per level.
Final Answer:
[1, 2, 3, 4] -> Option D
Quick Check:
Single node per level = all nodes visible [OK]
Hint: If no left child, right child is visible from left [OK]
Common Mistakes:
Assuming left side view is empty without left children
Ignoring right children as visible nodes
Thinking only left children count
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.