Left Side View of Binary Tree in DSA Javascript - Time & Space Complexity
We want to understand how the time needed to find the left side view of a binary tree changes as the tree grows.
How does the number of nodes affect the work done to get the left side view?
Analyze the time complexity of the following code snippet.
function leftSideView(root) {
if (!root) return [];
const result = [];
const queue = [root];
while (queue.length > 0) {
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;
}
This code finds the left side view by visiting nodes level by level and picking the first node at each level.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop runs once per level, and the inner for loop visits every node in the tree exactly once.
- How many times: Each node is processed once, so total operations grow with the number of nodes n.
As the tree grows, the number of nodes increases, and the code visits each node once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The work grows directly with the number of nodes, so doubling nodes roughly doubles the work.
Time Complexity: O(n)
This means the time to find the left side view grows linearly with the number of nodes in the tree.
[X] Wrong: "Since we only pick one node per level, the time is O(height) or O(log n)."
[OK] Correct: Even though we pick one node per level for the result, we still visit every node to know which is first at each level, so the time depends on all nodes, not just levels.
Understanding this helps you explain how tree traversal works and how to analyze algorithms that visit all nodes once, a key skill in many coding challenges.
"What if we used recursion instead of a queue for level order traversal? How would the time complexity change?"
Practice
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 CQuick Check:
Left side view = nodes visible from left [OK]
- Confusing left side view with right side view
- Including all leaf nodes instead of first nodes per level
- Thinking it shows deepest nodes only
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 AQuick Check:
Level order traversal uses queue [OK]
- Using stack which is LIFO, not suitable for level order
- Choosing set or map which don't maintain order
- Confusing queue with stack
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));Solution
Step 1: Traverse level by level and pick first node
Level 1: node 1 (first node), Level 2: node 2, Level 3: node 4Step 2: Collect these nodes in result array
Result array becomes [1, 2, 4]Final Answer:
[1, 2, 4] -> Option BQuick Check:
Left side nodes per level = [1, 2, 4] [OK]
- Picking right child instead of left
- Including all nodes instead of first per level
- Confusing order of nodes
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;
}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 AQuick Check:
Loop must run exactly levelSize times [OK]
- Using <= causes extra iteration and errors
- Confusing queue with stack for BFS
- Wrong index check for first node
1
\
2
\
3
\
4Solution
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 DQuick Check:
Single node per level = all nodes visible [OK]
- Assuming left side view is empty without left children
- Ignoring right children as visible nodes
- Thinking only left children count
Ready to prove it?
Pass the test and this pattern moves unknown
