Challenge - 5 Problems
Left Side View Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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); }
Attempts:
2 left
💡 Hint
The left side view shows the first node visible at each level from the left side.
✗ Incorrect
The left side view visits nodes level by level and picks the first node at each level. Here, level 0 has 1, level 1 has 2 (left child), and level 2 has 4 (left child of 2).
🧠 Conceptual
intermediateUnderstanding the Left Side View Concept
Which statement best describes the left side view of a binary tree?
Attempts:
2 left
💡 Hint
Think about what you see if you stand on the left side of the tree.
✗ Incorrect
The left side view is the set of nodes visible when looking at the tree from the left side, which means the first node encountered at each level.
🔧 Debug
advancedIdentify 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
}Attempts:
2 left
💡 Hint
Check how unwrap is used on Option values.
✗ Incorrect
If root is None, calling unwrap() on it causes a panic at runtime. The code does not check if root is None before unwrapping.
❓ Predict Output
advancedOutput 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); }
Attempts:
2 left
💡 Hint
Remember the left side view picks the first node at each level from left to right.
✗ Incorrect
At level 0: 10; level 1: 20 (only right child); level 2: 30 (right child of 20); level 3: 40 (left child of 30). All are first nodes at their levels.
🧠 Conceptual
expertTime 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?
Attempts:
2 left
💡 Hint
Consider how many nodes the algorithm visits in total.
✗ Incorrect
The algorithm visits every node once in a breadth-first manner, so the time complexity is linear in the number of nodes.
