Bird
Raised Fist0
DSA Rustprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Left Side View Mastery
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 for the given binary tree after running the Rust code?
DSA Rust
use std::collections::VecDeque;

#[derive(Debug)]
struct TreeNode {
    val: i32,
    left: Option<Box<TreeNode>>,
    right: Option<Box<TreeNode>>,
}

impl TreeNode {
    fn new(val: i32) -> Self {
        TreeNode { val, left: None, right: None }
    }
}

fn left_side_view(root: Option<Box<TreeNode>>) -> Vec<i32> {
    let mut result = Vec::new();
    if root.is_none() {
        return result;
    }
    let mut queue = VecDeque::new();
    queue.push_back(root);

    while !queue.is_empty() {
        let level_size = queue.len();
        for i in 0..level_size {
            let node_opt = queue.pop_front().unwrap();
            if let Some(node) = node_opt {
                if i == 0 {
                    result.push(node.val);
                }
                if node.left.is_some() {
                    queue.push_back(node.left.clone());
                }
                if node.right.is_some() {
                    queue.push_back(node.right.clone());
                }
            }
        }
    }
    result
}

fn main() {
    let mut root = Box::new(TreeNode::new(1));
    root.left = Some(Box::new(TreeNode::new(2)));
    root.right = Some(Box::new(TreeNode::new(3)));
    root.left.as_mut().unwrap().left = Some(Box::new(TreeNode::new(4)));
    root.right.as_mut().unwrap().right = Some(Box::new(TreeNode::new(5)));

    let view = left_side_view(Some(root));
    println!("{:?}", view);
}
A[1, 2, 5]
B[1, 2, 4]
C[1, 3, 5]
D[1, 3, 4]
Attempts:
2 left
💡 Hint
The left side view shows the first node visible at each level from the left side.
🧠 Conceptual
intermediate
1:30remaining
Understanding the Left Side View Concept
Which statement best describes the left side view of a binary tree?
AIt shows the nodes visible when the tree is viewed from the left side, picking the first node at each level.
BIt shows all leaf nodes of the binary tree.
CIt shows the nodes visible when the tree is viewed from the right side, picking the last node at each level.
DIt shows the nodes in the tree in in-order traversal.
Attempts:
2 left
💡 Hint
Think about what you see if you stand on the left side of the tree.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Left Side View Implementation
What error will occur when running the following Rust code for left side view of a binary tree?
DSA Rust
fn left_side_view(root: Option<Box<TreeNode>>) -> Vec<i32> {
    let mut result = Vec::new();
    let mut queue = VecDeque::new();
    queue.push_back(root.unwrap());

    while !queue.is_empty() {
        let level_size = queue.len();
        for i in 0..level_size {
            let node = queue.pop_front().unwrap();
            if i == 0 {
                result.push(node.val);
            }
            if node.left.is_some() {
                queue.push_back(node.left.unwrap());
            }
            if node.right.is_some() {
                queue.push_back(node.right.unwrap());
            }
        }
    }
    result
}
APanic at unwrap on empty Option if root is None
BIncorrect output because it pushes right child before left child
CInfinite loop because queue is never emptied
DCompilation error due to missing use of VecDeque
Attempts:
2 left
💡 Hint
Check how unwrap is used on Option values.
❓ Predict Output
advanced
2:00remaining
Output of Left Side View for Unbalanced Binary Tree
What is the output of the left side view function for this unbalanced binary tree?
DSA Rust
use std::collections::VecDeque;

#[derive(Debug)]
struct TreeNode {
    val: i32,
    left: Option<Box<TreeNode>>,
    right: Option<Box<TreeNode>>,
}

impl TreeNode {
    fn new(val: i32) -> Self {
        TreeNode { val, left: None, right: None }
    }
}

fn left_side_view(root: Option<Box<TreeNode>>) -> Vec<i32> {
    let mut result = Vec::new();
    if root.is_none() {
        return result;
    }
    let mut queue = VecDeque::new();
    queue.push_back(root);

    while !queue.is_empty() {
        let level_size = queue.len();
        for i in 0..level_size {
            let node_opt = queue.pop_front().unwrap();
            if let Some(node) = node_opt {
                if i == 0 {
                    result.push(node.val);
                }
                if node.left.is_some() {
                    queue.push_back(node.left.clone());
                }
                if node.right.is_some() {
                    queue.push_back(node.right.clone());
                }
            }
        }
    }
    result
}

fn main() {
    let mut root = Box::new(TreeNode::new(10));
    root.right = Some(Box::new(TreeNode::new(20)));
    root.right.as_mut().unwrap().right = Some(Box::new(TreeNode::new(30)));
    root.right.as_mut().unwrap().right.as_mut().unwrap().left = Some(Box::new(TreeNode::new(40)));

    let view = left_side_view(Some(root));
    println!("{:?}", view);
}
A[10, 30, 40]
B[10, 20]
C[10, 20, 30, 40]
D[10, 20, 40]
Attempts:
2 left
💡 Hint
Remember the left side view picks the first node at each level from left to right.
🧠 Conceptual
expert
1:30remaining
Time Complexity of Left Side View Algorithm
What is the time complexity of the standard breadth-first search approach to find the left side view of a binary tree with n nodes?
AO(1), because it only stores the first node of each level
BO(log n), because it only visits nodes on the left side
CO(n^2), because it processes each level separately
DO(n), because each node is visited exactly once
Attempts:
2 left
💡 Hint
Consider how many nodes the algorithm visits in total.

Practice

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

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 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 C
  4. Quick Check:

    Left side view = nodes seen from left [OK]
Hint: Think of standing on the left side and looking at the tree [OK]
Common Mistakes:
  • Confusing left side view with right side view
  • Including all leaf nodes instead of first nodes per level
  • Thinking it shows deepest nodes only
2. Which Rust code snippet correctly declares a binary tree node struct for left side view traversal?
easy
A. struct TreeNode { val: i32, left: TreeNode, right: TreeNode }
B. struct TreeNode { val: i32, left: Box<TreeNode>, right: Box<TreeNode> }
C. struct TreeNode { val: i32, left: &TreeNode, right: &TreeNode }
D. struct TreeNode { val: i32, left: Option, right: Option }

Solution

  1. Step 1: Check ownership and optional children

    Rust binary trees use Option for nullable child nodes.
  2. Step 2: Validate correct syntax

    Option allows for null or Some(child), which is correct for left and right.
  3. Final Answer:

    struct TreeNode { val: i32, left: Option, right: Option } -> Option D
  4. Quick Check:

    Use Option for nullable children [OK]
Hint: Use Option for child nodes in Rust [OK]
Common Mistakes:
  • Using direct TreeNode type for children (causes infinite size)
  • Using references without lifetimes
  • Omitting Option for nullable children
3. What is the output of the left side view function for this tree?

    1
   / \
  2   3
   \   \
    5   4
Assuming the function returns a vector of node values visible from the left side.
medium
A. [1, 3, 4]
B. [1, 2, 5]
C. [1, 2, 4]
D. [1, 5, 4]

Solution

  1. Step 1: Identify nodes visible from left at each level

    Level 0: 1, Level 1: 2 (left child), Level 2: 5 (right child of 2)
  2. Step 2: Collect visible nodes in order

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

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

    Left side nodes = [1, 2, 5] [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 order of nodes
4. Given this Rust function snippet for left side view, what is the bug?

fn left_side_view(root: Option) -> Vec<i32> {
    let mut result = vec![];
    let mut queue = vec![root.unwrap()];
    while !queue.is_empty() {
        let level_size = queue.len();
        for i in 0..level_size {
            let node = queue.remove(0);
            if i == 0 {
                result.push(node.val);
            }
            if let Some(left) = node.left {
                queue.push(left);
            }
            if let Some(right) = node.right {
                queue.push(right);
            }
        }
    }
    result
}
medium
A. Calling unwrap() on root without checking if it is null
B. Using remove(0) on vector causes runtime error
C. Pushing right child before left child breaks left view order
D. Not returning the result vector at the end

Solution

  1. Step 1: Check root unwrap usage

    Calling unwrap() on root without checking if it is null can cause panic if tree is empty.
  2. Step 2: Confirm other parts are correct

    remove(0) works but is inefficient; order of pushing left then right is correct; result is returned.
  3. Final Answer:

    Calling unwrap() on root without checking if it is null -> Option A
  4. Quick Check:

    unwrap() on null causes panic [OK]
Hint: Check unwrap() calls for null before use [OK]
Common Mistakes:
  • Ignoring unwrap panic possibility
  • Thinking remove(0) causes error (just slow)
  • Confusing child push order
  • Missing return statement
5. You want to modify the left side view function to return the leftmost node values only for levels where the node value is even. Which approach is correct?
hard
A. Add a check inside the level loop: if i == 0 and node.val % 2 == 0, then push node.val to result
B. Push node.val for all nodes during traversal, then filter even values from result after traversal
C. Push node.val only if node.val is even, ignoring position in level
D. Push node.val if it is even and it is the rightmost node at the level

Solution

  1. Step 1: Understand requirement for leftmost even nodes

    We want only the first node at each level if its value is even.
  2. Step 2: Apply condition during traversal

    Check if i == 0 (leftmost) and node.val % 2 == 0 before pushing to result.
  3. Final Answer:

    Add a check inside the level loop: if i == 0 and node.val % 2 == 0, then push node.val to result -> Option A
  4. Quick Check:

    Filter leftmost nodes by even value during traversal [OK]
Hint: Check leftmost and even condition together inside loop [OK]
Common Mistakes:
  • Filtering after traversal loses position info
  • Ignoring leftmost position and filtering only by value
  • Checking rightmost node instead of leftmost
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