Boundary Traversal means visiting the nodes on the boundary of the tree in an anti-clockwise direction starting from the root. It includes the left boundary, leaves, and right boundary.
Click to reveal answer
beginner
Which parts of the binary tree are included in the boundary traversal?
The boundary traversal includes three parts:
Left boundary (excluding leaves)
All leaf nodes (left to right)
Right boundary (excluding leaves, in reverse order)
Click to reveal answer
intermediate
Why do we exclude leaf nodes when traversing the left and right boundaries in boundary traversal?
Leaf nodes are excluded from left and right boundaries to avoid duplication because all leaf nodes are visited separately in the leaf traversal step.
Click to reveal answer
beginner
In Rust, what data structure is commonly used to store the boundary nodes during traversal before printing?
A Vec (vector) is commonly used to store nodes in order during traversal because it allows dynamic resizing and easy appending of nodes.
Click to reveal answer
intermediate
What is the time complexity of boundary traversal of a binary tree?
The time complexity is O(n), where n is the number of nodes, because each node is visited at most once during the traversal.
Click to reveal answer
Which of the following is NOT part of the boundary traversal of a binary tree?
ARight boundary excluding leaves
BAll leaf nodes
CAll nodes at the second level
DLeft boundary excluding leaves
✗ Incorrect
Only left boundary, leaf nodes, and right boundary are included. Nodes at the second level are included only if they are part of these boundaries.
In boundary traversal, how are the right boundary nodes added to the result?
AIn bottom-up order
BIn top-down order
CIn random order
DThey are not added
✗ Incorrect
Right boundary nodes are added in bottom-up order to maintain the anti-clockwise direction.
Which Rust collection type is best suited to store nodes during boundary traversal?
AHashMap
BVec
CLinkedList
DBTreeSet
✗ Incorrect
Vec is ideal for ordered storage and dynamic resizing during traversal.
What is the first node visited in boundary traversal of a binary tree?
ALeftmost leaf
BRight boundary node
CRightmost leaf
DRoot node
✗ Incorrect
Traversal starts from the root node.
If a binary tree has only one node, what is the boundary traversal output?
ARoot node only
BEmpty
CLeft boundary only
DRight boundary only
✗ Incorrect
With one node, the root is also the leaf and boundary, so output is the root node.
Explain the steps to perform boundary traversal of a binary tree.
Think about visiting edges and leaves separately.
You got /4 concepts.
Describe how you would implement boundary traversal in Rust using vectors.
Consider modular functions and vector appending.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the Boundary Traversal of Binary Tree?
easy
A. To print nodes in post-order traversal
B. To print nodes level by level
C. To print nodes in in-order traversal
D. To print all nodes on the boundary in anti-clockwise direction
Solution
Step 1: Understand boundary traversal definition
Boundary traversal prints nodes on the edges of the tree: left boundary, leaves, and right boundary.
Step 2: Identify traversal direction
The traversal is done in anti-clockwise order starting from root, left boundary, leaves, then right boundary.
Final Answer:
To print all nodes on the boundary in anti-clockwise direction -> Option D
Hint: Boundary traversal prints edges anti-clockwise [OK]
Common Mistakes:
Confusing boundary traversal with level order traversal
Including internal nodes not on boundary
Printing right boundary in clockwise order
2. Which of the following Rust function signatures is correct for a function that performs boundary traversal on a binary tree node defined as struct Node { val: i32, left: Option, right: Option }?
easy
A. fn boundary_traversal(root: Node) -> Vec<i32>
B. fn boundary_traversal(root: &Option) -> Vec<i32>
C. fn boundary_traversal(root: &Node) -> Vec<i32>
D. fn boundary_traversal(root: Option<Node>) -> Vec<i32>
Solution
Step 1: Analyze ownership and borrowing in Rust
Binary tree nodes are wrapped in Option to allow null and heap allocation. Passing by reference avoids ownership transfer.
Step 2: Match function signature with correct parameter type
Using &Option allows borrowing the root node safely without moving ownership.
Final Answer:
fn boundary_traversal(root: &Option) -> Vec<i32> -> Option B
Quick Check:
Borrow Option for traversal [OK]
Hint: Use &Option to borrow tree root safely [OK]
Common Mistakes:
Passing Node by value causes ownership move
Using Option<Node> instead of Option
Using &Node without Option wrapper
3. Given the following Rust code snippet for boundary traversal, what will be the output printed for the tree below?
Tree structure:
```
1
/ \
2 3
/ \ \
4 5 6
/ \ /
7 8 9
```
Code snippet:
Step 1: Identify boundary nodes in anti-clockwise order
Root: 1; Left boundary (excluding leaves): 2; Leaves (left-to-right): 4, 7, 8, 9; Right boundary (excluding leaves, bottom-up): 6, 3.
Step 2: Combine nodes without duplicates
Boundary traversal = [1, 2, 4, 7, 8, 9, 6, 3]
Final Answer:
[1, 2, 4, 7, 8, 9, 6, 3] -> Option A
Quick Check:
Boundary nodes = [1,2,4,7,8,9,6,3] [OK]
Hint: List left boundary, leaves, then right boundary reversed [OK]
Common Mistakes:
Including internal nodes like 5 in boundary
Adding right boundary top-down instead of bottom-up
Missing leaf nodes on right subtree
4. The following Rust function attempts to perform boundary traversal but has a bug. What is the main issue?
fn print_left_boundary(node: &Option) {
if let Some(n) = node {
if n.left.is_some() {
println!("{}", n.val);
print_left_boundary(&n.left);
} else if n.right.is_some() {
println!("{}", n.val);
print_left_boundary(&n.right);
}
}
}
medium
A. It incorrectly recurses on the right child when the left child is absent
B. It does not print the root node
C. It causes infinite recursion on leaf nodes
D. It skips nodes that have both left and right children
Solution
Step 1: Analyze recursion logic
The function recurses on the left child if present, but falls back to recursing on the right child if no left child. For left boundary, always prefer and recurse only on left child (stop if absent).
Step 2: Understand boundary traversal rule for left boundary
This incorrect fallback includes right subtree nodes in the left boundary traversal.
Final Answer:
It incorrectly recurses on the right child when the left child is absent -> Option A
Quick Check:
Left boundary: recurse left only [OK]
Hint: Always recurse on left child only for left boundary [OK]
Common Mistakes:
Recursing on right child when left is absent in left boundary
Not handling None nodes properly
Incorrectly printing leaves in left boundary
5. You want to modify the boundary traversal to print nodes in clockwise order starting from root. Which of the following changes is correct to achieve this?
hard
A. Print leaves first, then left boundary, then right boundary, then root last
B. Print root, then left boundary top-down, leaves left to right, then right boundary bottom-up
C. Print root, then right boundary top-down, leaves right to left, then left boundary bottom-up
D. Print right boundary bottom-up, leaves left to right, then left boundary top-down, root last
Solution
Step 1: Understand clockwise boundary traversal order
Clockwise traversal starts at root, then right boundary top-down, leaves from right to left, then left boundary bottom-up.
Step 2: Match the option with correct order
Print root, then right boundary top-down, leaves right to left, then left boundary bottom-up matches this order exactly, ensuring correct clockwise traversal.
Final Answer:
Print root, then right boundary top-down, leaves right to left, then left boundary bottom-up -> Option C
Quick Check:
Clockwise boundary = root + right + leaves + left reversed [OK]
Hint: Clockwise = root, right boundary down, leaves right-left, left boundary up [OK]
Common Mistakes:
Using anti-clockwise order for clockwise traversal
Printing leaves left to right instead of right to left
Reversing boundaries incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.