Challenge - 5 Problems
Boundary Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Boundary Traversal on 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
/* Boundary traversal prints nodes in this order: root, left boundary (excluding leaves), leaves, right boundary (excluding leaves) in reverse */ // Tree nodes: 1,2,3,4,5,6,7,8 // Expected output: 1 -> 2 -> 4 -> 7 -> 8 -> 6 -> 3 -> null
Attempts:
2 left
💡 Hint
Remember to exclude leaf nodes from left and right boundaries and include all leaves in left to right order.
✗ Incorrect
Boundary traversal includes root, left boundary excluding leaves, all leaves from left to right, and right boundary excluding leaves in reverse order. Here, left boundary is 2,4 (4 is leaf so excluded), leaves are 4,7,8,6, right boundary is 3 (excluding leaf 6). So final order is 1,2,4,7,8,6,3.
🧠 Conceptual
intermediateUnderstanding Leaf Nodes in Boundary Traversal
In boundary traversal of a binary tree, which of the following statements about leaf nodes is correct?
Attempts:
2 left
💡 Hint
Think about avoiding duplicates in the output.
✗ Incorrect
Leaf nodes are included exactly once in the boundary traversal to avoid duplicates. They are collected separately after left and right boundaries are processed.
❓ Predict Output
advancedOutput of Boundary Traversal on 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 left to right, right boundary excluding leaves reversed */ // Expected output: 10 -> 20 -> 40 -> 80 -> 60 -> 90 -> 70 -> 30 -> null
Attempts:
2 left
💡 Hint
Check which nodes are leaves and which are boundaries carefully.
✗ Incorrect
Left boundary excluding leaves: 20, 40 (40 is leaf so excluded), so only 20. Leaves: 40, 80, 60, 90. Right boundary excluding leaves: 30, 70 (70 is not leaf because it has right child 90), so 30, 70 reversed is 70, 30. Final order: 10, 20, 40, 80, 60, 90, 70, 30.
🔧 Debug
advancedIdentify the Error in Boundary Traversal Code Snippet
What error will the following C code snippet produce when performing boundary traversal on a binary tree?
Code snippet:
Code snippet:
void printLeftBoundary(Node* root) {
if (root == NULL) return;
if (root->left) {
printf("%d ", root->data);
printLeftBoundary(root->left);
} else if (root->right) {
printf("%d ", root->data);
printLeftBoundary(root->right);
}
}
void printLeaves(Node* root) {
if (root == NULL) return;
printLeaves(root->left);
if (root->left == NULL && root->right == NULL)
printf("%d ", root->data);
printLeaves(root->right);
}
void printRightBoundary(Node* root) {
if (root == NULL) return;
if (root->right) {
printRightBoundary(root->right);
printf("%d ", root->data);
} else if (root->left) {
printRightBoundary(root->left);
printf("%d ", root->data);
}
}
void printBoundary(Node* root) {
if (root == NULL) return;
printf("%d ", root->data);
printLeftBoundary(root->left);
printLeaves(root->left);
printLeaves(root->right);
printRightBoundary(root->right);
}Attempts:
2 left
💡 Hint
Check if left boundary printing excludes leaf nodes.
✗ Incorrect
The printLeftBoundary function prints all nodes with left or right child, but it does not exclude leaf nodes explicitly. So leaf nodes on left boundary get printed twice: once in left boundary and once in leaves.
🚀 Application
expertNumber of Nodes in Boundary Traversal
Given a binary tree with N nodes, what is the maximum number of nodes that can appear in its boundary traversal?
Attempts:
2 left
💡 Hint
Consider a skewed tree where all nodes form the boundary.
✗ Incorrect
In a skewed tree (like a linked list), all nodes are boundary nodes (left boundary, leaves, and right boundary overlap). So maximum nodes in boundary traversal can be all N nodes.
