Left Side View of Binary Tree in DSA C# - 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?
Analyze the time complexity of the following code snippet.
public IList<int> LeftSideView(TreeNode root) {
var result = new List<int>();
if (root == null) return result;
var queue = new Queue<TreeNode>();
queue.Enqueue(root);
while (queue.Count > 0) {
int levelSize = queue.Count;
for (int i = 0; i < levelSize; i++) {
var node = queue.Dequeue();
if (i == 0) result.Add(node.val);
if (node.left != null) queue.Enqueue(node.left);
if (node.right != null) queue.Enqueue(node.right);
}
}
return result;
}
This code finds the left side view by visiting each level of the tree and recording the first node at that level.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Visiting each node once in a breadth-first manner using a queue.
- How many times: Each node is enqueued and dequeued exactly once.
As the number of nodes increases, the code visits each node once, so the work grows directly with the number of nodes.
| 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: "The algorithm only looks at the left children, so it runs faster than visiting all nodes."
[OK] Correct: The code visits every node because it must check both left and right children to know the first node at each level. Skipping right children would miss nodes visible from the left side.
Understanding this linear time complexity helps you explain how tree traversal works efficiently and shows you can analyze common tree problems clearly.
"What if we changed the traversal to depth-first search instead of breadth-first? 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 AQuick Check:
Left side view = nodes seen from left [OK]
- Confusing left side view with right side view
- Thinking it shows all leaf nodes
- Assuming it shows nodes at the deepest level only
Solution
Step 1: Identify traversal method
Left side view is found by level order traversal, which processes nodes level by level.Step 2: Choose data structure for level order
A queue is used to hold nodes of each level in order for processing.Final Answer:
Queue -> Option CQuick Check:
Level order traversal uses queue [OK]
- Using stack which is for depth-first traversal
- Choosing dictionary which stores key-value pairs
- Using list without order control
1 / \ 2 3 / \ 4 5
Solution
Step 1: Identify nodes visible from left side
At level 1: node 1 is visible; level 2: node 2; level 3: node 4.Step 2: List nodes in order of levels
Left side view nodes are [1, 2, 4].Final Answer:
[1, 2, 4] -> Option AQuick Check:
Left side nodes = [1, 2, 4] [OK]
- Choosing right side nodes instead
- Including nodes not visible from left
- Reversing order of nodes
public IList<int> LeftSideView(TreeNode root) {
var result = new List<int>();
if (root == null) return result;
var queue = new Queue<TreeNode>();
queue.Enqueue(root);
while (queue.Count > 0) {
int size = queue.Count;
for (int i = 0; i < size; i++) {
var node = queue.Dequeue();
if (i == 0) result.Add(node.val);
if (node.left != null) queue.Enqueue(node.left);
if (node.right != null) queue.Enqueue(node.right);
}
}
return result;
}
What is the bug in this code?Solution
Step 1: Analyze the traversal logic
The code uses a queue for level order traversal and adds the first node at each level to result.Step 2: Verify correctness of node selection
Adding node when i == 0 ensures leftmost node at each level is added.Final Answer:
It correctly returns the left side view -> Option DQuick Check:
First node per level added = left side view [OK]
- Confusing queue with stack usage
- Adding last node instead of first
- Skipping root node
Solution
Step 1: Understand preorder traversal order
Preorder visits root, then left, then right child recursively.Step 2: Record first node at each depth
By visiting left child first, the first node at each depth is the leftmost node.Step 3: Skip recording if depth already visited
Only add node value if this is the first visit at that depth.Final Answer:
Traverse left child before right, and record the first node visited at each depth level -> Option BQuick Check:
Left-first preorder + first node per depth = left side view [OK]
- Visiting right child first loses leftmost nodes
- Recording all nodes instead of first per level
- Recording only leaf nodes misses intermediate nodes
Ready to prove it?
Pass the test and this pattern moves unknown
