Boundary Traversal means visiting the nodes on the outer edge of the tree in a specific order: left boundary (top to bottom), leaves (left to right), and right boundary (bottom to top).
Click to reveal answer
beginner
Which parts of the binary tree are included in the boundary traversal?
The boundary traversal includes three parts: 1) Left boundary nodes (excluding leaves), 2) All leaf nodes, 3) Right boundary nodes (excluding leaves).
Click to reveal answer
intermediate
Why do we exclude leaf nodes when collecting left and right boundaries in boundary traversal?
Leaf nodes are excluded from left and right boundaries to avoid duplication because all leaf nodes are collected separately in the leaf nodes step.
Click to reveal answer
intermediate
In boundary traversal, how is the right boundary collected differently from the left boundary?
The right boundary is collected from bottom to top, so nodes are stored in reverse order before adding to the result, unlike the left boundary which is collected top to bottom.
Click to reveal answer
beginner
Show a simple JavaScript function signature for boundary traversal of a binary tree.
function boundaryTraversal(root) {
// root is the root node of the binary tree
// returns an array of node values in boundary order
}
Click to reveal answer
Which nodes are visited first in boundary traversal of a binary tree?
ALeft boundary nodes from top to bottom
BLeaf nodes from left to right
CRight boundary nodes from bottom to top
DRoot node only
✗ Incorrect
Boundary traversal starts with the left boundary nodes from top to bottom.
Why do we collect leaf nodes separately in boundary traversal?
ABecause leaf nodes are not part of the tree
BTo avoid duplication of leaf nodes in left and right boundaries
CTo avoid missing any leaf nodes
DLeaf nodes are collected last for sorting
✗ Incorrect
Leaf nodes are collected separately to avoid duplication since they can appear in both left and right boundaries.
How is the right boundary added to the result in boundary traversal?
AFrom bottom to top
BFrom top to bottom
CIn random order
DRight boundary is not included
✗ Incorrect
Right boundary nodes are added from bottom to top to maintain the correct boundary order.
Which of these nodes is NOT included in the left boundary during boundary traversal?
ARoot node
BLeft child nodes excluding leaves
CLeaf nodes
DNodes on the left edge
✗ Incorrect
Leaf nodes are excluded from the left boundary and collected separately.
What is the time complexity of boundary traversal in a binary tree with n nodes?
AO(log n)
BO(1)
CO(n^2)
DO(n)
✗ Incorrect
Boundary traversal visits each node at most once, so the time complexity is O(n).
Explain the steps involved in performing a boundary traversal of a binary tree.
Think about the three parts of the boundary and how to avoid duplicates.
You got /5 concepts.
Describe how you would implement boundary traversal in code using simple helper functions.
Divide the problem into smaller parts for clarity.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of Boundary Traversal in a binary tree?
easy
A. To print all leaf nodes only
B. To print all nodes on the boundary in an anti-clockwise direction
C. To print nodes level by level
D. To print nodes in in-order traversal
Solution
Step 1: Understand boundary traversal definition
Boundary traversal prints nodes on the edges of the tree, including left boundary, leaves, and right boundary.
Step 2: Compare options with definition
Only To print all nodes on the boundary in an anti-clockwise direction describes printing all boundary nodes in anti-clockwise order.
Final Answer:
To print all nodes on the boundary in an anti-clockwise direction -> Option B
Hint: List left boundary, leaves, then right boundary reversed [OK]
Common Mistakes:
Including internal nodes as leaves
Adding right boundary leaves twice
Wrong order of nodes
4. Consider this code snippet for boundary traversal in JavaScript:
function printLeaves(node, result) {
if (!node) return;
if (!node.left && !node.right) {
result.push(node.data);
}
printLeaves(node.left, result);
printLeaves(node.right, result);
}
What is the bug in this code?
medium
A. It visits leaves in left to right order correctly
B. It pushes non-leaf nodes into result
C. It visits leaves in wrong order (right before left)
D. It does not check for null node before recursion
Solution
Step 1: Analyze leaf detection and recursion order
Code checks if node is leaf and pushes data, then recursively visits left then right child.
Step 2: Verify leaf order correctness
Visiting left subtree before right ensures leaves are collected left to right.
Step 3: Check for bugs
No bug found; code correctly collects leaves in left to right order.
Final Answer:
It visits leaves in left to right order correctly -> Option A
Quick Check:
Leaf nodes collected left to right = It visits leaves in left to right order correctly [OK]
Hint: Left subtree first ensures left-to-right leaf order [OK]
Common Mistakes:
Thinking recursion order is reversed
Assuming non-leaf nodes are pushed
Missing base case check
5. You want to perform boundary traversal on a binary tree but exclude all leaf nodes that have even values. Which approach correctly adapts the traversal?
hard
A. Modify leaf collection to skip leaves with even values during traversal
B. Skip left and right boundary nodes if they are even
C. Only collect leaves and ignore boundaries
D. Collect all nodes and filter even leaves after traversal
Solution
Step 1: Understand boundary traversal parts
Boundary traversal includes left boundary, leaves, and right boundary.
Step 2: Identify where to exclude even leaves
Only leaf nodes need filtering; boundaries remain unchanged.
Step 3: Adapt leaf collection function
Modify leaf collection to check leaf value and skip if even.
Final Answer:
Modify leaf collection to skip leaves with even values during traversal -> Option A
Quick Check:
Filter leaves during traversal, not boundaries [OK]
Hint: Filter leaves during leaf collection step only [OK]
Common Mistakes:
Filtering boundary nodes incorrectly
Filtering after traversal causing extra work
Ignoring leaf filtering altogether
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.