Challenge - 5 Problems
Boundary Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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
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?Attempts:
2 left
💡 Hint
Remember to exclude leaves from left and right boundaries and print leaves only once.
✗ Incorrect
Boundary traversal includes root, left boundary excluding leaves, all leaves from left to right, and right boundary excluding leaves printed bottom-up. Node 5 is not part of left or right boundary but has leaves 7 and 8.
🧠 Conceptual
intermediate1:00remaining
Understanding Leaf Nodes in Boundary Traversal
In boundary traversal of a binary tree, which nodes are considered leaf nodes?
Attempts:
2 left
💡 Hint
Think about what makes a node a leaf in any tree.
✗ Incorrect
Leaf nodes are nodes that do not have any children, meaning both left and right pointers are null.
❓ Predict Output
advanced2: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
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) */Attempts:
2 left
💡 Hint
Remember to print right boundary nodes bottom-up and include all leaves from left to right.
✗ Incorrect
Left boundary excluding leaves: 20, 40 (70 is leaf). Leaves: 70, 50, 90. Right boundary excluding leaves: 60, 30 printed bottom-up as 60 then 30. Root is 10.
🔧 Debug
advanced1: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
}
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
}
Attempts:
2 left
💡 Hint
Check what happens when the node is a leaf (no left or right child).
✗ Incorrect
The code does not print leaf nodes because it only prints when left or right child exists. Leaves are skipped here.
🚀 Application
expert2: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?
Attempts:
2 left
💡 Hint
Count root, left boundary excluding leaves, leaves, and right boundary excluding leaves carefully.
✗ Incorrect
A perfect binary tree of height 3 has 15 nodes. Leaves are all nodes at level 3 (8 nodes). Left boundary excluding leaves has 2 nodes, right boundary excluding leaves has 2 nodes, plus root (1). Total = 1 + 2 + 8 + 2 = 13 but root is counted once, so total 14 nodes.