0
0
DSA C++programming~20 mins

Boundary Traversal of Binary Tree in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Boundary Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Boundary Traversal for a Simple Tree
What is the output of the boundary traversal for the following binary tree?

Tree structure:
1
/ \
2 3
/ \ \
4 5 6
/ \
7 8
DSA C++
struct Node {
    int data;
    Node* left;
    Node* right;
};

// Boundary traversal prints nodes in this order:
// root, left boundary (excluding leaves), leaves, right boundary (excluding leaves, bottom-up)

// Given the tree above, what is the printed boundary traversal sequence?
A[1, 2, 4, 7, 8, 6, 3]
B[1, 2, 4, 7, 8, 6, 3, 5]
C[1, 2, 4, 5, 7, 8, 6, 3]
D[1, 2, 4, 7, 8, 5, 6, 3]
Attempts:
2 left
💡 Hint
Remember to exclude leaves from left and right boundaries and print leaves only once.
🧠 Conceptual
intermediate
1:00remaining
Understanding Leaf Nodes in Boundary Traversal
In boundary traversal of a binary tree, which nodes are considered leaf nodes?
ANodes on the left boundary only
BNodes with only one child
CNodes with no children (no left and no right child)
DNodes on the right boundary only
Attempts:
2 left
💡 Hint
Think about what makes a node a leaf in any tree.
Predict Output
advanced
2:30remaining
Output of Boundary Traversal for a Complex Tree
What is the output of the boundary traversal for the following binary tree?

Tree structure:
10
/ \
20 30
/ / \
40 50 60
\ /
70 80
\
90
DSA C++
/* Boundary traversal order: root, left boundary (excluding leaves), leaves, right boundary (excluding leaves, bottom-up) */
A[10, 20, 40, 70, 50, 90, 80, 60]
B[10, 20, 40, 70, 50, 80, 90, 60, 30]
C[10, 20, 40, 70, 50, 90, 80, 30, 60]
D[10, 20, 40, 70, 50, 90, 80, 60, 30]
Attempts:
2 left
💡 Hint
Remember to print right boundary nodes bottom-up and include all leaves from left to right.
🔧 Debug
advanced
1:30remaining
Identify the Error in Boundary Traversal Code
Given this snippet of C++ code for printing the left boundary of a binary tree, what error will it cause?

Code:
void printLeftBoundary(Node* root) {
if (!root) return;
if (root->left) {
cout << root->data << " ";
printLeftBoundary(root->left);
} else if (root->right) {
cout << root->data << " ";
printLeftBoundary(root->right);
}
// No else to handle leaf nodes
}
ALeaves on the left boundary will not be printed at all
BLeaves on the left boundary will be printed twice
CCode will cause a segmentation fault
DCode will print right boundary nodes instead of left
Attempts:
2 left
💡 Hint
Check what happens when the node is a leaf (no left or right child).
🚀 Application
expert
2:00remaining
Number of Nodes in Boundary Traversal
Consider a perfect binary tree of height 3 (levels 0 to 3). How many nodes will be printed in its boundary traversal?
A10
B14
C12
D15
Attempts:
2 left
💡 Hint
Count root, left boundary excluding leaves, leaves, and right boundary excluding leaves carefully.