Left Side View of Binary Tree in DSA C++ - 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.
void leftSideView(TreeNode* root, vector<int>& result) {
if (!root) return;
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
TreeNode* node = q.front(); q.pop();
if (i == 0) result.push_back(node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
}
}
This code visits each node level by level and records the first node of each level to get the left side view.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Visiting each node once in a breadth-first manner.
- How many times: Exactly once per node, since each node is enqueued and dequeued once.
As the number of nodes (n) increases, the code visits each node once, so the work grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The work grows linearly as the tree size grows.
Time Complexity: O(n)
This means the time to find the left side view grows directly with the number of nodes in the tree.
[X] Wrong: "The code only visits the left children, so it runs faster than visiting all nodes."
[OK] Correct: The code actually visits every node once, including right children, because it needs to check all nodes to find the leftmost at each level.
Understanding this linear time complexity helps you explain how tree traversals work efficiently and shows you can analyze level-order algorithms 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 shows nodes visible when looking at the tree from the left side.Step 2: Identify visible nodes
Only the first node encountered 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 DQuick Check:
Left side view = nodes visible from left [OK]
- Confusing left side view with right side view
- Thinking it shows all leaf nodes
- Assuming it shows deepest nodes only
Solution
Step 1: Check pointer usage for child nodes
Child nodes must be pointers to allow dynamic linking in a tree.Step 2: Validate data types
Data is int, left and right children are pointers to Node, matching correct binary tree node structure.Final Answer:
struct Node { int data; Node* left; Node* right; }; -> Option AQuick Check:
Use pointers for child nodes in tree struct [OK]
- Using Node instead of Node* for children
- Using int type for child nodes
- Omitting pointers causing infinite size error
1
/ \
2 3
/ \ \
4 5 6
What is the left side view output of this tree?Solution
Step 1: Identify nodes visible from left at each level
Level 1: 1, Level 2: 2 (left child), Level 3: 4 (left child of 2)Step 2: Collect nodes in order
Left side view nodes are [1, 2, 4]Final Answer:
[1, 2, 4] -> Option BQuick Check:
Left side view nodes = [1, 2, 4] [OK]
- Choosing right child nodes instead of left
- Including nodes not visible from left
- Mixing nodes from different levels
void leftViewUtil(Node* root, int level, int &maxLevel) {
if (!root) return;
if (level > maxLevel) {
cout << root->data << " ";
maxLevel = level;
}
leftViewUtil(root->left, level + 1, maxLevel);
leftViewUtil(root->right, level + 1, maxLevel);
}
void leftView(Node* root) {
int maxLevel = 0;
leftViewUtil(root, 1, maxLevel);
}
Solution
Step 1: Analyze traversal order and maxLevel usage
The function visits left child first, then right child, printing first node at each level.Step 2: Check initialization and printing logic
maxLevel initialized to 0 and level starts at 1, so first level prints correctly; printing before recursion is correct.Final Answer:
No error; function correctly prints left side view -> Option CQuick Check:
Correct preorder traversal prints left view [OK]
- Changing recursion order breaks left view logic
- Incorrect maxLevel initialization causing missed nodes
- Printing after recursion misses first nodes
Solution
Step 1: Understand problem with missing left children
Left side view must include visible nodes even if left child is missing but right child exists.Step 2: Choose traversal that captures first visible node per level
Level order traversal visits nodes level by level, so picking first node at each level ensures correct left side view.Final Answer:
Traverse level order and pick first node at each level -> Option AQuick Check:
Level order first node per level = left side view [OK]
- Ignoring right child when left child is missing
- Using preorder which may miss visible nodes
- Confusing inorder or postorder with left view logic
Ready to prove it?
Pass the test and this pattern moves unknown
