Challenge - 5 Problems
Left Side View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Left Side View for a Simple Binary Tree
What is the output of the following C++ code that prints the left side view of a binary tree?
DSA C++
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
#include <iostream>
#include <queue>
#include <vector>
std::vector<int> leftSideView(Node* root) {
std::vector<int> result;
if (!root) return result;
std::queue<Node*> q;
q.push(root);
while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; ++i) {
Node* node = q.front(); q.pop();
if (i == 0) result.push_back(node->data);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
}
return result;
}
int main() {
Node* root = new Node(10);
root->left = new Node(20);
root->right = new Node(30);
root->left->left = new Node(40);
root->left->right = new Node(50);
root->right->right = new Node(60);
std::vector<int> view = leftSideView(root);
for (int val : view) std::cout << val << " ";
return 0;
}Attempts:
2 left
💡 Hint
Remember, left side view shows the first node at each level from the left.
✗ Incorrect
The left side view prints the first node encountered at each level when viewed from the left side. Here, the nodes are 10 (root), 20 (left child of root), and 40 (left child of 20).
🧠 Conceptual
intermediate1:00remaining
Understanding 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 how you see the tree from the left side.
✗ Incorrect
The left side view includes the first node visible at each level when looking from the left side, which is the first node encountered at each level during a level order traversal.
🔧 Debug
advanced2:00remaining
Identify the Bug in Left Side View Implementation
What error will the following code produce when trying to print the left side view of a binary tree?
DSA C++
std::vector<int> leftSideView(Node* root) { std::vector<int> result; if (!root) return result; std::queue<Node*> q; q.push(root); while (!q.empty()) { int size = q.size(); for (int i = 0; i <= size; ++i) { Node* node = q.front(); q.pop(); if (i == 0) result.push_back(node->data); if (node->left) q.push(node->left); if (node->right) q.push(node->right); } } return result; }
Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
✗ Incorrect
The for loop uses 'i <= size' which causes one extra iteration beyond the queue size, leading to accessing q.front() when the queue is empty, causing a runtime error.
🚀 Application
advanced2:00remaining
Left Side View for a Skewed Binary Tree
Given a binary tree where every node has only a right child, what will be the left side view output?
DSA C++
Node* root = new Node(1); root->right = new Node(2); root->right->right = new Node(3); root->right->right->right = new Node(4); std::vector<int> view = leftSideView(root); for (int val : view) std::cout << val << " ";
Attempts:
2 left
💡 Hint
Even if nodes are only on the right, the left side view shows the first node at each level.
✗ Incorrect
Since each level has only one node (on the right), the left side view will include all nodes: 1, 2, 3, 4.
❓ Predict Output
expert3:00remaining
Output of Left Side View with Mixed Children
What is the output of the following code printing the left side view of this binary tree?
DSA C++
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
#include <iostream>
#include <queue>
#include <vector>
std::vector<int> leftSideView(Node* root) {
std::vector<int> result;
if (!root) return result;
std::queue<Node*> q;
q.push(root);
while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; ++i) {
Node* node = q.front(); q.pop();
if (i == 0) result.push_back(node->data);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
}
return result;
}
int main() {
Node* root = new Node(5);
root->left = new Node(3);
root->right = new Node(8);
root->left->right = new Node(4);
root->right->left = new Node(7);
root->right->right = new Node(9);
std::vector<int> view = leftSideView(root);
for (int val : view) std::cout << val << " ";
return 0;
}Attempts:
2 left
💡 Hint
At each level, the first node from the left is included.
✗ Incorrect
Level 1: 5 (root), Level 2: 3 (left child), Level 3: 4 (left->right child is the first node at level 3).