Left Side View of Binary Tree in DSA Java - Time & Space Complexity
We want to know 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?
Analyze the time complexity of the following code snippet.
public List leftSideView(TreeNode root) {
List result = new ArrayList<>();
if (root == null) return result;
Queue queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
if (i == 0) result.add(node.val);
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(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: Visiting each node once in a breadth-first manner.
- How many times: Each node is processed exactly one time.
As the number of nodes increases, the work grows directly with the number of nodes because each node is visited once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The operations grow linearly with the number of nodes.
Time Complexity: O(n)
This means the time to find the left side view grows in direct proportion to 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 all nodes to know which is first at each level. So the total work depends on all nodes, not just the height.
Understanding this helps you explain how tree traversal costs grow and shows you can analyze level-by-level processing clearly.
"What if we used a depth-first search instead of breadth-first search? How would the time complexity change?"
Practice
Solution
Step 1: Understand the view perspective
The left side view means looking at the tree from the left side, so only nodes visible from that side are included.Step 2: Identify what nodes are visible
Nodes that are the first in each level from the left are visible, so the leftmost nodes at each depth form the left side view.Final Answer:
Nodes visible when the tree is viewed from the left side -> Option AQuick Check:
Left side view = nodes visible from left side [OK]
- Confusing left side view with right side view
- Including all leaf nodes instead of leftmost nodes
- Thinking it shows deepest nodes only
Solution
Step 1: Identify traversal method for levels
Level order traversal visits nodes level by level, which is needed to find leftmost nodes at each level.Step 2: Choose data structure for level order
Queue is used for breadth-first search (level order traversal) because it processes nodes in FIFO order.Final Answer:
Queue -> Option AQuick Check:
Level order traversal uses queue [OK]
- Using stack which is for depth-first traversal
- Using HashMap which stores key-value pairs, not order
- Using ArrayList without order control
1
/ \
2 3
/ \
4 5
Solution
Step 1: Identify leftmost nodes at each level
Level 0: Node 1 is leftmost.
Level 1: Node 2 is leftmost.
Level 2: Node 4 is leftmost.Step 2: Collect nodes in order
Left side view nodes are [1, 2, 4].Final Answer:
[1, 2, 4] -> Option DQuick Check:
Left side nodes = [1, 2, 4] [OK]
- Choosing right child nodes instead of left
- Including nodes not visible from left
- Mixing order of nodes
void leftSideView(Node root) {
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
Node curr = q.poll();
if (i == 0) System.out.print(curr.data + " ");
if (curr.left != null) q.add(curr.left);
if (curr.right != null) q.add(curr.right);
}
}
}
Solution
Step 1: Check root handling
The code adds root to queue without checking if root is null, which can cause NullPointerException.Step 2: Verify other parts
Using poll() is correct to remove and get node.
Printing only when i==0 prints leftmost node.
Loop condition is correct for level size.Final Answer:
No check if root is null before adding to queue -> Option CQuick Check:
Null root check missing causes error [OK]
- Ignoring null root causing runtime error
- Confusing poll() with peek() usage
- Printing all nodes instead of leftmost only
Solution
Step 1: Understand normal left side view collection
Normally, leftmost nodes are collected in order from top to bottom during level order traversal.Step 2: Modify to reverse order
Store leftmost nodes in a stack during traversal, then pop all to get bottom-to-top order.Final Answer:
Use a stack to store leftmost nodes during level order, then pop all -> Option BQuick Check:
Stack reverses order of collected nodes [OK]
- Trying to reverse queue order directly
- Not storing nodes before printing
- Using recursion without reversing result
Ready to prove it?
Pass the test and this pattern moves unknown
