Challenge - 5 Problems
Right Side View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Right Side View for a Simple Binary Tree
What is the output of the right side view function for the given binary tree?
DSA Rust
struct TreeNode {
val: i32,
left: Option<Box<TreeNode>>,
right: Option<Box<TreeNode>>,
}
fn right_side_view(root: Option<Box<TreeNode>>) -> Vec<i32> {
use std::collections::VecDeque;
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 = queue.pop_front().unwrap();
if let Some(n) = node {
if i == level_size - 1 {
result.push(n.val);
}
if n.left.is_some() {
queue.push_back(n.left.clone());
}
if n.right.is_some() {
queue.push_back(n.right.clone());
}
}
}
}
result
}
fn main() {
let root = Some(Box::new(TreeNode {
val: 1,
left: Some(Box::new(TreeNode {
val: 2,
left: None,
right: Some(Box::new(TreeNode {
val: 5,
left: None,
right: None,
})),
})),
right: Some(Box::new(TreeNode {
val: 3,
left: None,
right: Some(Box::new(TreeNode {
val: 4,
left: None,
right: None,
})),
})),
}));
let view = right_side_view(root);
println!("{:?}", view);
}Attempts:
2 left
💡 Hint
The right side view shows the last node visible at each level from the right side.
✗ Incorrect
At level 1, node 1 is visible. At level 2, node 3 is the rightmost visible. At level 3, node 4 is the rightmost visible node.
🧠 Conceptual
intermediateUnderstanding the Right Side View Concept
Which statement best describes the right side view of a binary tree?
Attempts:
2 left
💡 Hint
Think about what nodes you see if you look at the tree from the right side.
✗ Incorrect
The right side view includes the rightmost node at each depth level, as seen from the right side of the tree.
🔧 Debug
advancedIdentify the Error in Right Side View Implementation
What error will occur when running the following Rust code for right side view of a binary tree?
DSA Rust
fn right_side_view(root: Option<Box<TreeNode>>) -> Vec<i32> {
use std::collections::VecDeque;
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 == level_size - 1 {
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 what happens if root is None and unwrap is called.
✗ Incorrect
Calling unwrap on None causes a panic at runtime. The code does not check if root is None before unwrap.
❓ Predict Output
advancedOutput of Right Side View for Unbalanced Tree
What is the output of the right side view function for this unbalanced binary tree?
DSA Rust
let root = Some(Box::new(TreeNode {
val: 10,
left: Some(Box::new(TreeNode {
val: 5,
left: Some(Box::new(TreeNode {
val: 3,
left: None,
right: None,
})),
right: None,
})),
right: Some(Box::new(TreeNode {
val: 15,
left: None,
right: Some(Box::new(TreeNode {
val: 20,
left: None,
right: None,
})),
})),
}));
let view = right_side_view(root);
println!("{:?}", view);Attempts:
2 left
💡 Hint
The right side view shows the rightmost node at each level, even if the tree is unbalanced.
✗ Incorrect
Level 1 rightmost is 10, level 2 rightmost is 15, level 3 rightmost is 20.
🚀 Application
expertNumber of Nodes in Right Side View for Complete Binary Tree of Height 4
How many nodes will be in the right side view of a complete binary tree with height 4?
Attempts:
2 left
💡 Hint
The right side view has one node per level visible from the right side.
✗ Incorrect
A complete binary tree of height 4 has 4 levels, so the right side view has 4 nodes.
