Bird
Raised Fist0
DSA C++programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the "Left Side View" of a binary tree represent?
easy
A. Nodes at the deepest level of the tree
B. Nodes visible when the tree is viewed from the right side
C. All leaf nodes of the tree
D. Nodes visible when the tree is viewed from the left side

Solution

  1. Step 1: Understand the view perspective

    The left side view shows nodes visible when looking at the tree from the left side.
  2. Step 2: Identify visible nodes

    Only the first node encountered at each level from the left is visible in this view.
  3. Final Answer:

    Nodes visible when the tree is viewed from the left side -> Option D
  4. Quick Check:

    Left side view = nodes visible from left [OK]
Hint: Left side view shows first node at each level from left [OK]
Common Mistakes:
  • Confusing left side view with right side view
  • Thinking it shows all leaf nodes
  • Assuming it shows deepest nodes only
2. Which of the following C++ code snippets correctly declares a node structure for a binary tree used in left side view traversal?
easy
A. struct Node { int data; Node* left; Node* right; };
B. struct Node { int data; Node left; Node right; };
C. struct Node { int data; Node* left; int right; };
D. struct Node { int data; int left; int right; };

Solution

  1. Step 1: Check pointer usage for child nodes

    Child nodes must be pointers to allow dynamic linking in a tree.
  2. Step 2: Validate data types

    Data is int, left and right children are pointers to Node, matching correct binary tree node structure.
  3. Final Answer:

    struct Node { int data; Node* left; Node* right; }; -> Option A
  4. Quick Check:

    Use pointers for child nodes in tree struct [OK]
Hint: Use pointers for left and right child nodes in struct [OK]
Common Mistakes:
  • Using Node instead of Node* for children
  • Using int type for child nodes
  • Omitting pointers causing infinite size error
3. Consider the following binary tree:

      1
     / \
    2   3
   / \   \
  4   5   6
What is the left side view output of this tree?
medium
A. [1, 3, 6]
B. [1, 2, 4]
C. [1, 2, 5]
D. [1, 2, 6]

Solution

  1. 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)
  2. Step 2: Collect nodes in order

    Left side view nodes are [1, 2, 4]
  3. Final Answer:

    [1, 2, 4] -> Option B
  4. Quick Check:

    Left side view nodes = [1, 2, 4] [OK]
Hint: Pick first node at each level from left [OK]
Common Mistakes:
  • Choosing right child nodes instead of left
  • Including nodes not visible from left
  • Mixing nodes from different levels
4. The following C++ function is intended to print the left side view of a binary tree. What is the error?

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);
}
medium
A. The order of recursive calls should be right child first, then left child
B. maxLevel should be initialized to -1 instead of 0
C. No error; function correctly prints left side view
D. The function should print after both recursive calls

Solution

  1. Step 1: Analyze traversal order and maxLevel usage

    The function visits left child first, then right child, printing first node at each level.
  2. 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.
  3. Final Answer:

    No error; function correctly prints left side view -> Option C
  4. Quick Check:

    Correct preorder traversal prints left view [OK]
Hint: Left child first recursion prints left view correctly [OK]
Common Mistakes:
  • Changing recursion order breaks left view logic
  • Incorrect maxLevel initialization causing missed nodes
  • Printing after recursion misses first nodes
5. Given a binary tree where some levels have missing left children but have right children, which approach ensures correct left side view output?
hard
A. Traverse level order and pick first node at each level
B. Traverse preorder and print nodes only if left child exists
C. Traverse inorder and print nodes at odd levels
D. Traverse postorder and print leaf nodes only

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Traverse level order and pick first node at each level -> Option A
  4. Quick Check:

    Level order first node per level = left side view [OK]
Hint: Use level order traversal, pick first node at each level [OK]
Common Mistakes:
  • Ignoring right child when left child is missing
  • Using preorder which may miss visible nodes
  • Confusing inorder or postorder with left view logic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard