0
0
DSA C++programming~20 mins

Left Side View of Binary Tree in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Left Side View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A10 20 40
B10 30 60
C10 20 50
D10 30 50
Attempts:
2 left
💡 Hint
Remember, left side view shows the first node at each level from the left.
🧠 Conceptual
intermediate
1:00remaining
Understanding Left Side View Node Selection
In the left side view of a binary tree, which nodes are included in the output?
AAll nodes with only left children
BThe first node encountered at each level from the left
CAll leaf nodes
DThe rightmost node at each level
Attempts:
2 left
💡 Hint
Think about how you see the tree from the left side.
🔧 Debug
advanced
2: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;
}
ACompilation error due to incorrect loop syntax
BNo error, outputs correct left side view
CRuntime error: queue is empty when calling q.front()
DInfinite loop causing program to hang
Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
🚀 Application
advanced
2: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 << " ";
A1 2 3 4
B1
C4
D2 3 4
Attempts:
2 left
💡 Hint
Even if nodes are only on the right, the left side view shows the first node at each level.
Predict Output
expert
3: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;
}
A5 3 7
B5 8 9
C5 8 7
D5 3 4
Attempts:
2 left
💡 Hint
At each level, the first node from the left is included.