Challenge - 5 Problems
Preorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Preorder Traversal on a Simple Binary Tree
What is the output of the preorder traversal (Root, Left, Right) for the following binary tree?
Tree structure:
1
/ \ 2 3
/
4
Tree structure:
1
/ \ 2 3
/
4
DSA C
struct Node {
int data;
struct Node* left;
struct Node* right;
};
void preorder(struct Node* root) {
if (root == NULL) return;
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
// Tree creation omitted for brevityAttempts:
2 left
💡 Hint
Remember preorder means visit root first, then left subtree, then right subtree.
✗ Incorrect
Preorder traversal visits the root node first, then recursively visits the left subtree, followed by the right subtree. So the order is 1 (root), 2 (left child), 4 (left child's left child), then 3 (right child).
🧠 Conceptual
intermediateNumber of Nodes Visited in Preorder Traversal
Given a binary tree with 7 nodes, how many nodes will be visited during a complete preorder traversal?
Attempts:
2 left
💡 Hint
Preorder traversal visits every node exactly once.
✗ Incorrect
Preorder traversal visits every node in the tree exactly once, so for 7 nodes, it visits all 7.
🔧 Debug
advancedIdentify the Bug in Preorder Traversal Code
What error will the following preorder traversal code produce when run on a non-empty tree?
DSA C
void preorder(struct Node* root) {
if (root == NULL) return;
preorder(root->left);
printf("%d ", root->data);
preorder(root->right);
}Attempts:
2 left
💡 Hint
Check the order of operations inside the function.
✗ Incorrect
The code visits left subtree first, then prints root, then right subtree, which is inorder traversal, not preorder.
📝 Syntax
advancedSyntax Error in Preorder Traversal Function
Which option contains a syntax error in the preorder traversal function?
DSA C
void preorder(struct Node* root) {
if (root == NULL) return;
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}Attempts:
2 left
💡 Hint
Check for missing semicolons after statements.
✗ Incorrect
Option A is missing a semicolon after 'return' statement causing a syntax error.
🚀 Application
expertPreorder Traversal Output of Complex Tree
Given the binary tree below, what is the preorder traversal output?
Tree structure:
10
/ \ 5 15
/ \ \ 3 7 18
\ 8
Tree structure:
10
/ \ 5 15
/ \ \ 3 7 18
\ 8
Attempts:
2 left
💡 Hint
Remember preorder visits root first, then left subtree, then right subtree recursively.
✗ Incorrect
Preorder visits 10 (root), then left subtree (5, 3, 7, 8), then right subtree (15, 18).
