Challenge - 5 Problems
Left Side View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Left Side View for a Simple Binary Tree
What is the output of the left side view function for the given binary tree?
DSA C
struct Node {
int data;
struct Node* left;
struct Node* right;
};
void leftViewUtil(struct Node* root, int level, int* maxLevel) {
if (root == NULL) return;
if (*maxLevel < level) {
printf("%d ", root->data);
*maxLevel = level;
}
leftViewUtil(root->left, level + 1, maxLevel);
leftViewUtil(root->right, level + 1, maxLevel);
}
void leftView(struct Node* root) {
int maxLevel = 0;
leftViewUtil(root, 1, &maxLevel);
}
// Tree structure:
// 1
// / \
// 2 3
// \ \
// 4 5
int main() {
struct Node n1 = {1, NULL, NULL};
struct Node n2 = {2, NULL, NULL};
struct Node n3 = {3, NULL, NULL};
struct Node n4 = {4, NULL, NULL};
struct Node n5 = {5, NULL, NULL};
n1.left = &n2;
n1.right = &n3;
n2.right = &n4;
n3.right = &n5;
leftView(&n1);
return 0;
}Attempts:
2 left
💡 Hint
The left side view prints the first node visible at each level from the left side.
✗ Incorrect
The function prints nodes 1 at level 1, 2 at level 2 (left child), and 4 at level 3 (leftmost visible node). Nodes 3 and 5 are not visible from the left side at their levels.
🧠 Conceptual
intermediateUnderstanding Left Side View Node Selection
In the left side view of a binary tree, which nodes are included in the output?
Attempts:
2 left
💡 Hint
Think about what you see when looking at the tree from the left side.
✗ Incorrect
The left side view includes the first node visible at each level when viewed from the left side, which is the leftmost node at that level.
🔧 Debug
advancedIdentify the Bug in Left Side View Implementation
What error will this code produce when trying to print the left side view of a binary tree?
DSA C
void leftViewUtil(struct Node* root, int level, int* maxLevel) { if (root == NULL) return; if (maxLevel < level) { printf("%d ", root->data); *maxLevel = level; } leftViewUtil(root->left, level + 1, maxLevel); leftViewUtil(root->right, level + 1, maxLevel); }
Attempts:
2 left
💡 Hint
Check how maxLevel is compared in the if condition.
✗ Incorrect
The condition 'if (maxLevel < level)' compares a pointer to an int, which is invalid and causes a compilation error.
🚀 Application
advancedLeft Side View of a Complete Binary Tree
Given a complete binary tree with nodes numbered 1 to 7 as below, what is the left side view output?
1
/ \
2 3
/ \ / \
4 5 6 7
Attempts:
2 left
💡 Hint
The left side view shows the first node visible at each level from the left side.
✗ Incorrect
At level 1, node 1 is visible; at level 2, node 2 is the leftmost; at level 3, node 4 is the leftmost node visible.
❓ Predict Output
expertOutput of Left Side View for Unbalanced Binary Tree
What is the output of the left side view function for the following unbalanced binary tree?
10
\
20
/ \
30 40
\
50
DSA C
struct Node {
int data;
struct Node* left;
struct Node* right;
};
void leftViewUtil(struct Node* root, int level, int* maxLevel) {
if (root == NULL) return;
if (*maxLevel < level) {
printf("%d ", root->data);
*maxLevel = level;
}
leftViewUtil(root->left, level + 1, maxLevel);
leftViewUtil(root->right, level + 1, maxLevel);
}
void leftView(struct Node* root) {
int maxLevel = 0;
leftViewUtil(root, 1, &maxLevel);
}
int main() {
struct Node n10 = {10, NULL, NULL};
struct Node n20 = {20, NULL, NULL};
struct Node n30 = {30, NULL, NULL};
struct Node n40 = {40, NULL, NULL};
struct Node n50 = {50, NULL, NULL};
n10.right = &n20;
n20.left = &n30;
n20.right = &n40;
n40.right = &n50;
leftView(&n10);
return 0;
}Attempts:
2 left
💡 Hint
Remember the left side view prints the first node visible at each level from the left side.
✗ Incorrect
Level 1: 10; Level 2: 20 (only right child of 10); Level 3: 30 (left child of 20); Level 4: 50 (right child of 40, left child is NULL so 50 is visible). Node 40 at level 3 is not visible from left side because 30 is first.
