Challenge - 5 Problems
Boundary Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Boundary Traversal on a Simple Binary Tree
What is the output of the boundary traversal for the following binary tree?
Tree structure:
1
/ \
2 3
/ \ \
4 5 6
/ \
7 8
Tree structure:
1
/ \
2 3
/ \ \
4 5 6
/ \
7 8
DSA Rust
struct Node {
val: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
fn boundary_traversal(root: &Option<Box<Node>>) -> Vec<i32> {
fn is_leaf(node: &Option<Box<Node>>) -> bool {
if let Some(n) = node {
n.left.is_none() && n.right.is_none()
} else {
false
}
}
fn add_left_boundary(node: &Option<Box<Node>>, res: &mut Vec<i32>) {
let mut curr = node;
while let Some(n) = curr {
if !is_leaf(curr) {
res.push(n.val);
}
if n.left.is_some() {
curr = &n.left;
} else {
curr = &n.right;
}
}
}
fn add_leaves(node: &Option<Box<Node>>, res: &mut Vec<i32>) {
if let Some(n) = node {
if is_leaf(node) {
res.push(n.val);
} else {
add_leaves(&n.left, res);
add_leaves(&n.right, res);
}
}
}
fn add_right_boundary(node: &Option<Box<Node>>, res: &mut Vec<i32>) {
let mut curr = node;
let mut tmp = Vec::new();
while let Some(n) = curr {
if !is_leaf(curr) {
tmp.push(n.val);
}
if n.right.is_some() {
curr = &n.right;
} else {
curr = &n.left;
}
}
for val in tmp.into_iter().rev() {
res.push(val);
}
}
let mut res = Vec::new();
if root.is_none() {
return res;
}
let root_node = root.as_ref().unwrap();
if !is_leaf(root) {
res.push(root_node.val);
}
add_left_boundary(&root_node.left, &mut res);
add_leaves(root, &mut res);
add_right_boundary(&root_node.right, &mut res);
res
}
fn main() {
let root = Some(Box::new(Node {
val: 1,
left: Some(Box::new(Node {
val: 2,
left: Some(Box::new(Node { val: 4, left: None, right: None })),
right: Some(Box::new(Node {
val: 5,
left: Some(Box::new(Node { val: 7, left: None, right: None })),
right: Some(Box::new(Node { val: 8, left: None, right: None })),
})),
})),
right: Some(Box::new(Node {
val: 3,
left: None,
right: Some(Box::new(Node { val: 6, left: None, right: None })),
})),
}));
let result = boundary_traversal(&root);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Remember boundary traversal includes left boundary (excluding leaves), all leaves, then right boundary (excluding leaves) in reverse.
✗ Incorrect
The boundary traversal starts with root (1), then left boundary nodes excluding leaves (2), then all leaves from left to right (4,7,8,6), then right boundary nodes excluding leaves in reverse (3). So the output is [1, 2, 4, 7, 8, 6, 3].
❓ Predict Output
intermediateBoundary Traversal Output for Single Node Tree
What is the output of the boundary traversal for a binary tree with only one node (value 10)?
DSA Rust
struct Node {
val: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
fn boundary_traversal(root: &Option<Box<Node>>) -> Vec<i32> {
fn is_leaf(node: &Option<Box<Node>>) -> bool {
if let Some(n) = node {
n.left.is_none() && n.right.is_none()
} else {
false
}
}
fn add_left_boundary(node: &Option<Box<Node>>, res: &mut Vec<i32>) {
let mut curr = node;
while let Some(n) = curr {
if !is_leaf(curr) {
res.push(n.val);
}
if n.left.is_some() {
curr = &n.left;
} else {
curr = &n.right;
}
}
}
fn add_leaves(node: &Option<Box<Node>>, res: &mut Vec<i32>) {
if let Some(n) = node {
if is_leaf(node) {
res.push(n.val);
} else {
add_leaves(&n.left, res);
add_leaves(&n.right, res);
}
}
}
fn add_right_boundary(node: &Option<Box<Node>>, res: &mut Vec<i32>) {
let mut curr = node;
let mut tmp = Vec::new();
while let Some(n) = curr {
if !is_leaf(curr) {
tmp.push(n.val);
}
if n.right.is_some() {
curr = &n.right;
} else {
curr = &n.left;
}
}
for val in tmp.into_iter().rev() {
res.push(val);
}
}
let mut res = Vec::new();
if root.is_none() {
return res;
}
let root_node = root.as_ref().unwrap();
if !is_leaf(root) {
res.push(root_node.val);
}
add_left_boundary(&root_node.left, &mut res);
add_leaves(root, &mut res);
add_right_boundary(&root_node.right, &mut res);
res
}
fn main() {
let root = Some(Box::new(Node { val: 10, left: None, right: None }));
let result = boundary_traversal(&root);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
A single node is also a leaf and the root, so it should appear once.
✗ Incorrect
Since the tree has only one node which is a leaf and root, the boundary traversal returns [10].
🔧 Debug
advancedIdentify the Bug in Right Boundary Traversal
The following Rust function is intended to add the right boundary nodes (excluding leaves) of a binary tree to the result vector in reverse order. What is the bug causing incorrect output?
DSA Rust
fn add_right_boundary(node: &Option<Box<Node>>, res: &mut Vec<i32>) {
let mut curr = node;
let mut tmp = Vec::new();
while let Some(n) = curr {
if !is_leaf(curr) {
tmp.push(n.val);
}
if n.left.is_some() {
curr = &n.left;
} else {
curr = &n.right;
}
}
for val in tmp.into_iter().rev() {
res.push(val);
}
}Attempts:
2 left
💡 Hint
Right boundary traversal should prioritize right child over left child.
✗ Incorrect
The bug is that the code moves to left child first, which is wrong for right boundary traversal. It should move to right child first to correctly collect right boundary nodes.
🧠 Conceptual
advancedUnderstanding Leaf Node Inclusion in Boundary Traversal
In boundary traversal of a binary tree, why do we add leaf nodes separately after adding left boundary nodes and before adding right boundary nodes?
Attempts:
2 left
💡 Hint
Think about how left and right boundaries exclude leaves to avoid duplication.
✗ Incorrect
Left and right boundary traversals exclude leaf nodes to prevent duplicates. Leaf nodes are added separately to include all leaves exactly once.
🚀 Application
expertNumber of Nodes in Boundary Traversal Result
Given a perfect binary tree of height 3 (levels 0 to 3), how many nodes will be in the boundary traversal result?
Attempts:
2 left
💡 Hint
Count left boundary excluding leaves, all leaves, and right boundary excluding leaves, plus root.
✗ Incorrect
A perfect binary tree of height 3 has 15 nodes. Leaves are at level 3 (8 leaves). Left boundary excluding leaves has 2 nodes, right boundary excluding leaves has 2 nodes, plus root (1). Total = 1 + 2 + 8 + 2 = 13.
