📖 Scenario: You are working with a binary tree data structure that represents a hierarchy of tasks in a project. You want to collect the tasks that lie on the boundary of this tree to review the outermost tasks first.
🎯 Goal: Build a program that performs a boundary traversal of a binary tree. The boundary traversal visits the nodes on the left boundary (top to bottom), then all leaf nodes (left to right), and finally the nodes on the right boundary (bottom to top).
📋 What You'll Learn
Create a binary tree with the exact structure given.
Add a helper variable to store the boundary nodes.
Implement the boundary traversal logic using functions and loops.
Print the boundary traversal result as a list of node values.
💡 Why This Matters
🌍 Real World
Boundary traversal helps in scenarios where you want to process or review the outermost elements of hierarchical data, such as project tasks, organizational charts, or file systems.
💼 Career
Understanding tree traversals and boundary traversal is useful for software engineers working with hierarchical data structures, UI trees, and algorithms that require selective node processing.
Progress0 / 4 steps
1
Create the Binary Tree Structure
Create a binary tree using the Node class with this exact structure: root node with value 20, left child 8, right child 22, left child of 8 is 4, right child of 8 is 12, left child of 12 is 10, right child of 12 is 14, right child of 22 is 25.
DSA Javascript
Hint
Use a class called Node with data, left, and right properties. Create nodes and link them exactly as described.
2
Create an Array to Store Boundary Nodes
Create an empty array called boundaryNodes to store the boundary traversal nodes.
DSA Javascript
Hint
Use const boundaryNodes = []; to create an empty array.
3
Implement Boundary Traversal Logic
Write functions isLeaf(node), addLeftBoundary(node), addLeaves(node), and addRightBoundary(node) to collect boundary nodes. Then call these functions in order to fill boundaryNodes with the boundary traversal of the tree starting from root.
DSA Javascript
Hint
Use helper functions to add left boundary, leaves, and right boundary nodes. Add root first if it is not a leaf.
4
Print the Boundary Traversal Result
Print the boundaryNodes array to display the boundary traversal of the binary tree.
DSA Javascript
Hint
Use console.log(boundaryNodes); to print the final boundary traversal array.
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.