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.
Specifically, how does the number of nodes affect the work done?
Analyze the time complexity of the following code snippet.
void leftSideViewHelper(TreeNode* root, int level, int* maxLevel) {
if (root == NULL) return;
if (level > *maxLevel) {
printf("%d ", root->val);
*maxLevel = level;
}
leftSideViewHelper(root->left, level + 1, maxLevel);
leftSideViewHelper(root->right, level + 1, maxLevel);
}
void leftSideView(TreeNode* root) {
int maxLevel = 0;
leftSideViewHelper(root, 1, &maxLevel);
}
This code prints the left side view of a binary tree by visiting nodes level by level, prioritizing left children.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive traversal visiting each node once.
- How many times: Exactly once per node in the tree.
As the number of nodes grows, the function 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 work grows linearly as the tree size increases.
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: "Since we only print one node per level, the time is O(height) or O(log n)."
[OK] Correct: Even though we print one node per level, we still visit every node to check if it is the first at its level, so the time depends on all nodes, not just the height.
Understanding this time complexity helps you explain how tree traversals work and why visiting all nodes is necessary, a common skill in coding interviews.
"What if we changed the traversal to visit right children before left? How would the time complexity change?"
Practice
Left Side View of a binary tree represent?Solution
Step 1: Understand the view concept
The left side view shows nodes visible when looking at the tree from the left side.Step 2: Differentiate from other views
Right side view shows nodes from the right, leaf nodes are only the bottom nodes, and deepest level nodes are not necessarily visible from left.Final Answer:
Nodes visible when the tree is viewed from the left side -> Option AQuick 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 correct struct declaration
typedef struct Node { int data; struct Node* left; struct Node* right; } Node; correctly declares a struct with pointers to left and right child nodes.Step 2: Identify errors in other options
struct Node { int data; Node* left; Node* right; }; typedef Node; misses struct keyword before Node pointers, C uses struct Node instead of pointers causing infinite size, D misses struct name for pointers.Final Answer:
typedef struct Node { int data; struct Node* left; struct Node* right; } Node; -> Option BQuick Check:
Correct struct with pointers = typedef struct Node { int data; struct Node* left; struct Node* right; } Node; [OK]
- Using struct members instead of pointers causing infinite size
- Missing struct keyword before pointer types
- Incorrect typedef syntax
1
/ \
2 3
/ \
4 5
What is the left side view output printed by this C code?void leftViewUtil(Node* root, int level, int* maxLevel) {
if (!root) return;
if (level > *maxLevel) {
printf("%d ", 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: Trace recursive calls and printing
At level 1, print 1; level 2, print 2 (left child); level 3, print 4 (left child of 2). Right children at same levels are ignored since maxLevel is updated.Step 2: Confirm output sequence
Printed nodes are 1, 2, 4 in order with spaces.Final Answer:
1 2 4 -> Option CQuick Check:
Left side view nodes = 1 2 4 [OK]
- Printing right child nodes before left
- Not updating maxLevel correctly
- Confusing left side view with right side view
void leftViewUtil(Node* root, int level, int* maxLevel) {
if (root == NULL) return;
if (level >= *maxLevel) {
printf("%d ", root->data);
*maxLevel = level;
}
leftViewUtil(root->left, level + 1, maxLevel);
leftViewUtil(root->right, level + 1, maxLevel);
}Solution
Step 1: Analyze the condition for printing
The condition 'level >= *maxLevel' causes printing nodes multiple times at the same level, which is incorrect.Step 2: Correct condition usage
It should be 'level > *maxLevel' to print only the first node encountered at each level.Final Answer:
The condition should be 'level > *maxLevel' instead of 'level >= *maxLevel' -> Option AQuick Check:
Use strict greater than for first node per level [OK]
- Using '>=' causes duplicate prints at same level
- Printing after recursion changes order
- Incorrect base case logic
1. Enqueue root
2. While queue not empty:
a. Get number of nodes at current level (size)
b. For i in 0 to size-1:
- Dequeue node
- If i == 0, print node data
- Enqueue left child if exists
- Enqueue right child if exists
What is the output for this tree? 10
/ \
20 30
\ \
40 50
Solution
Step 1: Perform level order traversal with printing first node per level
Level 1: print 10; Level 2: print 20 (first node at level); Level 3: print 40 (first node at level).Step 2: Confirm output sequence
Nodes printed in order: 10 20 40 with spaces.Final Answer:
10 20 40 -> Option DQuick Check:
Left side view by level order = 10 20 40 [OK]
- Printing last node instead of first at each level
- Mixing left and right child enqueue order
- Not counting nodes per level correctly
Ready to prove it?
Pass the test and this pattern moves unknown
