Bird
Raised Fist0
DSA Cprogramming~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 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;
}
A1 3 5
B1 2 4
C1 2 3 4 5
D1 4 5
Attempts:
2 left
💡 Hint
The left side view prints the first node visible at each level from the left side.
🧠 Conceptual
intermediate
1:30remaining
Understanding Left Side View Node Selection
In the left side view of a binary tree, which nodes are included in the output?
AAll leaf nodes on the left subtree
BAll nodes that have a left child
CThe leftmost node at each level of the tree
DThe rightmost node at each level of the tree
Attempts:
2 left
💡 Hint
Think about what you see when looking at the tree from the left side.
🔧 Debug
advanced
2:00remaining
Identify 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);
}
ACompilation error due to incorrect pointer comparison
BRuntime error due to null pointer dereference
CNo output printed because maxLevel is never updated
DCorrect output printed without errors
Attempts:
2 left
💡 Hint
Check how maxLevel is compared in the if condition.
🚀 Application
advanced
1:30remaining
Left 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
A1 2 5
B1 4 6
C1 3 7
D1 2 4
Attempts:
2 left
💡 Hint
The left side view shows the first node visible at each level from the left side.
❓ Predict Output
expert
2:30remaining
Output 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;
}
A10 20 30 50
B10 30 40 50
C10 20 40 50
D10 20 30 40 50
Attempts:
2 left
💡 Hint
Remember the left side view prints the first node visible at each level from the left side.

Practice

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

Solution

  1. Step 1: Understand the view concept

    The left side view shows nodes visible when looking at the tree from the left side.
  2. 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.
  3. Final Answer:

    Nodes visible when the tree is viewed from the left side -> Option A
  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 binary tree node structure for computing the left side view?
easy
A. struct Node { int data; struct Node left; struct Node right; };
B. typedef struct Node { int data; struct Node* left; struct Node* right; } Node;
C. struct Node { int data; Node* left; Node* right; }; typedef Node;
D. typedef struct { int data; struct Node* left; struct Node* right; } Node;

Solution

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

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

    Correct struct with pointers = typedef struct Node { int data; struct Node* left; struct Node* right; } Node; [OK]
Hint: Use pointers for left and right child nodes in struct [OK]
Common Mistakes:
  • Using struct members instead of pointers causing infinite size
  • Missing struct keyword before pointer types
  • Incorrect typedef syntax
3. Given the following binary tree:
      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);
}
medium
A. 1 3 5
B. 1 2 5
C. 1 2 4
D. 1 4 5

Solution

  1. 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.
  2. Step 2: Confirm output sequence

    Printed nodes are 1, 2, 4 in order with spaces.
  3. Final Answer:

    1 2 4 -> Option C
  4. Quick Check:

    Left side view nodes = 1 2 4 [OK]
Hint: Print first node at each level during preorder left-first traversal [OK]
Common Mistakes:
  • Printing right child nodes before left
  • Not updating maxLevel correctly
  • Confusing left side view with right side view
4. Identify the bug in this C function intended to print the left side view of a binary tree:
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);
}
medium
A. The condition should be 'level > *maxLevel' instead of 'level >= *maxLevel'
B. The function should print after recursive calls, not before
C. The base case should check 'if (root != NULL)' instead of 'if (root == NULL)'
D. The maxLevel pointer should be passed by value, not by reference

Solution

  1. Step 1: Analyze the condition for printing

    The condition 'level >= *maxLevel' causes printing nodes multiple times at the same level, which is incorrect.
  2. Step 2: Correct condition usage

    It should be 'level > *maxLevel' to print only the first node encountered at each level.
  3. Final Answer:

    The condition should be 'level > *maxLevel' instead of 'level >= *maxLevel' -> Option A
  4. Quick Check:

    Use strict greater than for first node per level [OK]
Hint: Use '>' not '>=' to print only first node at each level [OK]
Common Mistakes:
  • Using '>=' causes duplicate prints at same level
  • Printing after recursion changes order
  • Incorrect base case logic
5. You want to print the left side view of a binary tree using a queue for level order traversal. Which approach correctly achieves this?
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
hard
A. 10 40 50
B. 10 30 50
C. 10 20 50
D. 10 20 40

Solution

  1. 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).
  2. Step 2: Confirm output sequence

    Nodes printed in order: 10 20 40 with spaces.
  3. Final Answer:

    10 20 40 -> Option D
  4. Quick Check:

    Left side view by level order = 10 20 40 [OK]
Hint: Print first node at each level during BFS traversal [OK]
Common Mistakes:
  • Printing last node instead of first at each level
  • Mixing left and right child enqueue order
  • Not counting nodes per level correctly
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