It is a way to visit all the nodes on the boundary of a binary tree. This includes the left boundary (top to bottom), all leaf nodes (left to right), and the right boundary (bottom to top).
Click to reveal answer
beginner
Which nodes are included in the left boundary during boundary traversal?
All nodes on the path from the root to the left-most leaf, excluding leaf nodes if they are already counted in the leaf nodes section.
Click to reveal answer
intermediate
Why do we exclude leaf nodes from the left and right boundaries when doing boundary traversal?
Because leaf nodes are already included separately in the leaf nodes section to avoid duplication in the traversal output.
Click to reveal answer
intermediate
In boundary traversal, how is the right boundary collected and added to the result?
The right boundary nodes are collected from the bottom up (excluding leaf nodes) and added after the leaf nodes to maintain the correct order.
Click to reveal answer
beginner
What is the time complexity of boundary traversal of a binary tree with n nodes?
The time complexity is O(n) 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?
ALeft boundary nodes
BAll internal nodes
CRight boundary nodes
DAll leaf nodes
✗ Incorrect
Boundary traversal includes left boundary, leaf nodes, and right boundary, but not all internal nodes.
In boundary traversal, leaf nodes are visited in which order?
ABottom to top
BRight to left
CTop to bottom
DLeft to right
✗ Incorrect
Leaf nodes are visited from left to right to maintain the boundary order.
When collecting the right boundary nodes, how should they be added to the final output?
ABottom to top
BLeft to right
CTop to bottom
DRandom order
✗ Incorrect
Right boundary nodes are added from bottom to top to maintain the correct boundary order.
What is the first step in performing boundary traversal of a binary tree?
AAdd the root node
BTraverse the right boundary
CTraverse all leaf nodes
DTraverse the left boundary
✗ Incorrect
The root node is added first as it is part of the boundary.
If a binary tree has only one node, what will be the boundary traversal output?
AEmpty
BLeft boundary nodes
COnly the root node
DRight boundary nodes
✗ Incorrect
If there is only one node, it is both the root and a leaf, so the output is just that node.
Explain the steps to perform boundary traversal of a binary tree.
Think about the order in which boundary nodes appear around the tree.
You got /4 concepts.
Describe why leaf nodes are handled separately in boundary traversal.
Consider how nodes could be counted twice if not careful.
You got /4 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 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: Identify traversal direction
The traversal is done in anti-clockwise direction starting from root, left boundary, leaves, then right boundary.
Final Answer:
To print all nodes on the boundary in anti-clockwise direction -> Option B
Hint: Boundary traversal prints edges anti-clockwise [OK]
Common Mistakes:
Confusing boundary traversal with leaf node traversal
Thinking it prints nodes level-wise
Assuming it prints in-order nodes
2. Which of the following TypeScript function signatures correctly represents a function to perform boundary traversal of a binary tree?
easy
A. function boundaryTraversal(): number[]
B. function boundaryTraversal(root: TreeNode): void
C. function boundaryTraversal(root: TreeNode, result: number[]): void
D. function boundaryTraversal(root: TreeNode): number[]
Solution
Step 1: Analyze function purpose
The function should take the root node and return an array of numbers representing boundary nodes.
Step 2: Check signatures
function boundaryTraversal(root: TreeNode): number[] takes root and returns number array, matching expected usage. function boundaryTraversal(root: TreeNode): void returns void, so no output. function boundaryTraversal(root: TreeNode, result: number[]): void requires external array input, less common. function boundaryTraversal(): number[] has no input, so invalid.
Final Answer:
function boundaryTraversal(root: TreeNode): number[] -> Option D
Quick Check:
Function returns boundary nodes array [OK]
Hint: Function should return array of boundary nodes [OK]
Common Mistakes:
Using void return type without output
Missing root parameter
Expecting external array input unnecessarily
3. Given the binary tree below, what is the boundary traversal output?
1
/ \
2 3
/ \ \
4 5 6
/ \ /
7 8 9
medium
A. [1, 2, 4, 7, 8, 9, 6]
B. [1, 2, 4, 5, 7, 8, 9, 6, 3]
C. [1, 2, 4, 7, 8, 9, 6, 3]
D. [1, 2, 4, 7, 8, 9, 3, 6]
Solution
Step 1: Identify left boundary nodes
Left boundary excluding leaves: 1, 2
Step 2: Identify leaf nodes left to right
Leaves: 4, 7, 8, 9, 6
Step 3: Identify right boundary nodes bottom to top excluding leaves
Right boundary: 3
Step 4: Combine in anti-clockwise order
Boundary = left boundary + leaves (excluding duplicates) + right boundary = [1, 2, 4, 7, 8, 9, 6, 3]
Final Answer:
[1, 2, 4, 7, 8, 9, 6, 3] -> Option C
Quick Check:
Boundary traversal = [1,2,4,7,8,9,6,3] [OK]
Hint: Combine left boundary, leaves, right boundary anti-clockwise [OK]
Common Mistakes:
Including leaf nodes twice
Wrong order of right boundary nodes
Missing root or boundary nodes
4. Consider the following TypeScript code snippet for boundary traversal. What is the bug?
function printBoundary(root: TreeNode | null) {
if (!root) return;
console.log(root.val);
printLeftBoundary(root.left);
printLeaves(root.left);
printLeaves(root.right);
printRightBoundary(root.right);
}
function printLeftBoundary(node: TreeNode | null) {
if (!node || (!node.left && !node.right)) return;
console.log(node.val);
if (node.left) printLeftBoundary(node.left);
else printLeftBoundary(node.right);
}
function printRightBoundary(node: TreeNode | null) {
if (!node || (!node.left && !node.right)) return;
if (node.right) printRightBoundary(node.right);
else printRightBoundary(node.left);
console.log(node.val);
}
medium
A. Left boundary includes leaf nodes which should be excluded
B. Root node is printed twice
C. Right boundary nodes are printed before recursive calls, causing wrong order
D. Leaves are printed twice because printLeaves is called on both left and right subtrees
Solution
Step 1: Analyze left boundary function
printLeftBoundary prints node.val even if node is leaf, but leaves should be excluded from left boundary.
Step 2: Check leaf exclusion condition
Condition (!node.left && !node.right) returns early, so leaves are excluded from printing in left boundary, so no bug here.
Step 3: Re-examine leaf printing calls
printLeaves is called separately on left and right subtrees, so leaves are printed once only.
Step 4: Check right boundary printing order
Right boundary prints after recursive calls, which is correct for bottom-up order.
Step 5: Identify actual bug
Left boundary function prints node.val before recursive call, so leaf nodes on left boundary get printed twice: once in left boundary and once in leaves.
Final Answer:
Left boundary includes leaf nodes which should be excluded -> Option A
Quick Check:
Leaf nodes must not print in left boundary [OK]
Hint: Exclude leaves from left/right boundary printing [OK]
Common Mistakes:
Printing leaves twice
Printing right boundary in wrong order
Printing root twice
5. You want to modify boundary traversal to print nodes in clockwise order starting from root. Which change is correct?
hard
A. Print root, then right boundary top-down, then leaves right to left, then left boundary bottom-up
B. Print root, then left boundary top-down, then leaves left to right, then right boundary bottom-up
C. Print root, then leaves left to right, then left boundary top-down, then right boundary bottom-up
D. Print root, then right boundary bottom-up, then leaves right to left, then left boundary top-down
Solution
Step 1: Understand clockwise boundary traversal order
Clockwise traversal starts at root, then right boundary top-down, leaves right to left, then left boundary bottom-up.
Step 2: Match options with traversal order
Print root, then right boundary top-down, then leaves right to left, then left boundary bottom-up matches this order exactly. Print root, then left boundary top-down, then leaves left to right, then right boundary bottom-up is anti-clockwise order. Options C and D mix orders incorrectly.
Final Answer:
Print root, then right boundary top-down, then leaves right to left, then left boundary bottom-up -> Option A
Quick Check:
Clockwise = root + right boundary + leaves + left boundary [OK]
Hint: Clockwise = root, right boundary, leaves, left boundary [OK]
Common Mistakes:
Confusing clockwise with anti-clockwise order
Printing boundaries in wrong direction
Mixing leaf traversal order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.