Challenge - 5 Problems
Vertical Order Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Vertical Order Traversal for a Simple Tree
What is the output of the vertical order traversal for the following binary tree?
Tree structure:
1
/ \
2 3
/ \
4 5
Tree structure:
1
/ \
2 3
/ \
4 5
DSA Rust
use std::collections::{BTreeMap, VecDeque};
#[derive(Debug)]
struct TreeNode {
val: i32,
left: Option<Box<TreeNode>>,
right: Option<Box<TreeNode>>,
}
fn vertical_order(root: Option<Box<TreeNode>>) -> Vec<Vec<i32>> {
if root.is_none() {
return vec![];
}
let mut column_table: BTreeMap<i32, Vec<i32>> = BTreeMap::new();
let mut queue: VecDeque<(Box<TreeNode>, i32)> = VecDeque::new();
queue.push_back((root.unwrap(), 0));
while let Some((node, col)) = queue.pop_front() {
column_table.entry(col).or_default().push(node.val);
if let Some(left) = node.left {
queue.push_back((left, col - 1));
}
if let Some(right) = node.right {
queue.push_back((right, col + 1));
}
}
column_table.into_values().collect()
}
fn main() {
let root = Some(Box::new(TreeNode {
val: 1,
left: Some(Box::new(TreeNode {
val: 2,
left: Some(Box::new(TreeNode { val: 4, left: None, right: None })),
right: None,
})),
right: Some(Box::new(TreeNode {
val: 3,
left: None,
right: Some(Box::new(TreeNode { val: 5, left: None, right: None })),
})),
}));
let result = vertical_order(root);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Think about columns from left to right and nodes in each column from top to bottom.
✗ Incorrect
The vertical order traversal groups nodes by their horizontal distance from the root. Here, 4 is at column -2, 2 at -1, 1 at 0, 3 at 1, and 5 at 2. So the output is [[4], [2], [1], [3], [5]].
❓ Predict Output
intermediateVertical Order Traversal Output with Multiple Nodes in Same Column
What is the output of the vertical order traversal for this binary tree?
Tree structure:
3
/ \
9 8
/ \ / \
4 0 1 7
Tree structure:
3
/ \
9 8
/ \ / \
4 0 1 7
DSA Rust
use std::collections::{BTreeMap, VecDeque};
#[derive(Debug)]
struct TreeNode {
val: i32,
left: Option<Box<TreeNode>>,
right: Option<Box<TreeNode>>,
}
fn vertical_order(root: Option<Box<TreeNode>>) -> Vec<Vec<i32>> {
if root.is_none() {
return vec![];
}
let mut column_table: BTreeMap<i32, Vec<i32>> = BTreeMap::new();
let mut queue: VecDeque<(Box<TreeNode>, i32)> = VecDeque::new();
queue.push_back((root.unwrap(), 0));
while let Some((node, col)) = queue.pop_front() {
column_table.entry(col).or_default().push(node.val);
if let Some(left) = node.left {
queue.push_back((left, col - 1));
}
if let Some(right) = node.right {
queue.push_back((right, col + 1));
}
}
column_table.into_values().collect()
}
fn main() {
let root = Some(Box::new(TreeNode {
val: 3,
left: Some(Box::new(TreeNode {
val: 9,
left: Some(Box::new(TreeNode { val: 4, left: None, right: None })),
right: Some(Box::new(TreeNode { val: 0, left: None, right: None })),
})),
right: Some(Box::new(TreeNode {
val: 8,
left: Some(Box::new(TreeNode { val: 1, left: None, right: None })),
right: Some(Box::new(TreeNode { val: 7, left: None, right: None })),
})),
}));
let result = vertical_order(root);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Remember nodes are grouped by column, and within each column, nodes appear top to bottom.
✗ Incorrect
Nodes 4 at column -2, 9 at -1, 3, 0, 1 at 0, 8 at 1, and 7 at 2. The order inside column 0 is top to bottom: 3, then 0, then 1.
🔧 Debug
advancedIdentify the Error in Vertical Order Traversal Implementation
The following Rust code attempts vertical order traversal but produces incorrect output. What is the main reason?
Code snippet:
Code snippet:
while let Some((node, col)) = queue.pop_front() {
column_table.entry(col).or_default().push(node.val);
if node.left.is_some() {
queue.push_back((node.left.unwrap(), col - 1));
}
if node.right.is_some() {
queue.push_back((node.right.unwrap(), col + 1));
}
}Attempts:
2 left
💡 Hint
Consider what happens if left or right child is None.
✗ Incorrect
Calling unwrap() on an Option that is None causes a runtime panic. The code checks is_some() but unwrap() is called twice separately, which can cause panic if the Option changes between calls or if unwrap is called without is_some check.
❓ Predict Output
advancedOutput of Vertical Order Traversal with Negative and Positive Columns
Given this binary tree, what is the vertical order traversal output?
Tree structure:
10
/ \
5 15
/ \ \
3 7 18
Tree structure:
10
/ \
5 15
/ \ \
3 7 18
DSA Rust
use std::collections::{BTreeMap, VecDeque};
#[derive(Debug)]
struct TreeNode {
val: i32,
left: Option<Box<TreeNode>>,
right: Option<Box<TreeNode>>,
}
fn vertical_order(root: Option<Box<TreeNode>>) -> Vec<Vec<i32>> {
if root.is_none() {
return vec![];
}
let mut column_table: BTreeMap<i32, Vec<i32>> = BTreeMap::new();
let mut queue: VecDeque<(Box<TreeNode>, i32)> = VecDeque::new();
queue.push_back((root.unwrap(), 0));
while let Some((node, col)) = queue.pop_front() {
column_table.entry(col).or_default().push(node.val);
if let Some(left) = node.left {
queue.push_back((left, col - 1));
}
if let Some(right) = node.right {
queue.push_back((right, col + 1));
}
}
column_table.into_values().collect()
}
fn main() {
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: Some(Box::new(TreeNode { val: 7, left: None, right: None })),
})),
right: Some(Box::new(TreeNode {
val: 15,
left: None,
right: Some(Box::new(TreeNode { val: 18, left: None, right: None })),
})),
}));
let result = vertical_order(root);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Remember columns are horizontal distances from root: left is negative, right is positive.
✗ Incorrect
Nodes 3 at column -2, 5 at -1, 10 and 7 at 0, 15 at 1, and 18 at 2. The order inside column 0 is top to bottom: 10 then 7.
🧠 Conceptual
expertUnderstanding Data Structures Used in Vertical Order Traversal
Which data structure is best suited to store nodes grouped by their vertical columns in vertical order traversal, ensuring columns are processed from left to right?
Options:
Options:
Attempts:
2 left
💡 Hint
Think about the need to keep columns sorted by their horizontal distance.
✗ Incorrect
BTreeMap keeps keys sorted, which allows easy retrieval of columns from left to right. HashMap is unordered, Vector can't handle negative indices easily, and LinkedList does not provide key-based access.
