📖 Scenario: Imagine you have a family tree represented as a binary tree. You want to list all family members who are on the outer edge of this tree, starting from the top ancestor, then the left side members, the leaf members, and finally the right side members in reverse order.
🎯 Goal: Build a program that performs a boundary traversal of a binary tree. The traversal should print the nodes on the boundary in this order: root, left boundary (excluding leaves), all leaves (left to right), and right boundary (excluding leaves) in reverse order.
📋 What You'll Learn
Create a binary tree with the exact structure given
Implement functions to print the left boundary, leaves, and right boundary
Combine these functions to perform the boundary traversal
Print the boundary traversal nodes in one line separated by spaces
💡 Why This Matters
🌍 Real World
Boundary traversal helps in graphical rendering of tree edges, network topology visualization, and understanding hierarchical data boundaries.
💼 Career
Understanding tree traversals and boundary conditions is essential for software engineers working with data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create the Binary Tree Structure
Create a struct called Node with an int data, and two pointers left and right. Then create the exact binary tree with root node having value 20, left child 8, right child 22, left child's left child 4, left child's right child 12, right child's right child 25, and left child's right child's left child 10 and left child's right child's right child 14.
DSA C++
Hint
Define the Node struct with a constructor for easy node creation. Then link nodes exactly as described.
2
Add Helper Functions to Check Leaf Nodes
Add a function called isLeaf that takes a Node* and returns true if the node has no left and right children, otherwise false.
DSA C++
Hint
Check if both left and right pointers are nullptr.
3
Implement Boundary Traversal Helper Functions
Implement three functions: printLeftBoundary(Node* root), printLeaves(Node* root), and printRightBoundary(Node* root).
- printLeftBoundary should print the left boundary nodes excluding leaves, top-down.
- printLeaves should print all leaf nodes from left to right.
- printRightBoundary should print the right boundary nodes excluding leaves, bottom-up.
Use cout to print node data followed by a space.
DSA C++
Hint
Use loops for left and right boundaries and recursion for leaves. Store right boundary nodes in a stack to print in reverse.
4
Perform and Print the Boundary Traversal
In main(), print the root node's data followed by a space. Then call printLeftBoundary(root), printLeaves(root), and printRightBoundary(root) in this order to print the full boundary traversal on one line.
DSA C++
Hint
Print root first, then call the three helper functions in order to print the boundary traversal.
Practice
(1/5)
1. What does the boundary traversal of a binary tree include?
easy
A. Left boundary, leaves, and right boundary nodes in order
B. Only leaf nodes from left to right
C. All nodes in level order
D. Only root and leaf nodes
Solution
Step 1: Understand boundary traversal components
Boundary traversal includes the left boundary (excluding leaves), all leaf nodes, and the right boundary (excluding leaves) in reverse order.
Step 2: Combine all parts in order
Traverse left boundary top-down, then leaves left to right, then right boundary bottom-up to get the full boundary traversal.
Final Answer:
Left boundary, leaves, and right boundary nodes in order -> Option A
Quick Check:
Boundary traversal = Left boundary + Leaves + Right boundary [OK]
Hint: Remember: left boundary + leaves + right boundary [OK]
Common Mistakes:
Including leaf nodes twice in left or right boundary
Ignoring right boundary order (should be bottom-up)
Confusing boundary traversal with level order
2. Which of the following C++ function signatures is correct for printing the left boundary of a binary tree?
easy
A. void printLeftBoundary(Node* root);
B. int printLeftBoundary(Node root);
C. void printLeftBoundary(Node root);
D. int printLeftBoundary(Node* root);
Solution
Step 1: Identify correct parameter type for tree node
Binary tree nodes are passed as pointers (Node*), not by value (Node), to avoid copying the entire subtree.
Step 2: Check return type for printing function
Printing functions usually return void as they output directly and do not return a value.
Left boundary + leaves + right boundary = [1, 2, 4, 7, 8, 9, 6, 3] [OK]
Hint: Left boundary + leaves + right boundary in order [OK]
Common Mistakes:
Including leaf nodes twice in boundary
Wrong order of right boundary nodes
Missing some leaf nodes
4. The following code snippet is intended to print the right boundary of a binary tree. What is the error?
void printRightBoundary(Node* root) {
if (!root) return;
if (root->right) {
printRightBoundary(root->right);
} else if (root->left) {
printRightBoundary(root->left);
}
std::cout << root->data << " ";
}
medium
A. It misses printing the root node
B. It does not print nodes in bottom-up order
C. It prints leaf nodes which should be excluded
D. It causes infinite recursion
Solution
Step 1: Analyze printing condition in recursion
The function prints every node after recursive calls, including leaf nodes which should be excluded from right boundary printing.
Step 2: Identify missing leaf node check
There is no check to skip printing leaf nodes, so leaf nodes get printed incorrectly as part of right boundary.
Final Answer:
It prints leaf nodes which should be excluded -> Option C
Quick Check:
Leaf nodes must be excluded from right boundary [OK]
Hint: Skip printing leaf nodes in boundary functions [OK]
Common Mistakes:
Printing leaf nodes in boundary traversal
Not checking for null nodes before recursion
Confusing order of printing nodes
5. You want to perform boundary traversal on a binary tree that may have only one node or be skewed (all left or all right). Which approach correctly handles these edge cases?
hard
A. Print only leaf nodes as boundary in skewed trees
B. Print all nodes in level order to cover all cases
C. Print left boundary including leaves, then right boundary including leaves, ignoring root
D. Print root, then left boundary excluding leaves, then leaves, then right boundary excluding leaves, skipping duplicates
Solution
Step 1: Understand edge cases in boundary traversal
Single node tree or skewed trees may cause duplicates if leaves are included in boundaries without checks.
Step 2: Use approach that prints root, left boundary excluding leaves, leaves, right boundary excluding leaves, and skip duplicates
This ensures no node is printed twice and all edge cases are handled correctly.
Final Answer:
Print root, then left boundary excluding leaves, then leaves, then right boundary excluding leaves, skipping duplicates -> Option D
Quick Check:
Exclude leaves in boundaries + skip duplicates [OK]
Hint: Exclude leaves in boundaries and skip duplicates [OK]
Common Mistakes:
Printing leaves twice in skewed trees
Ignoring root node in traversal
Using level order which is not boundary traversal
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.