Challenge - 5 Problems
Preorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Preorder Traversal on a Simple Binary Tree
What is the output of the preorder traversal (Root, Left, Right) of the following binary tree?
Tree structure:
1
/ \
2 3
/ \
4 5
Tree structure:
1
/ \
2 3
/ \
4 5
DSA C++
struct Node {
int val;
Node* left;
Node* right;
Node(int x) : val(x), left(nullptr), right(nullptr) {}
};
void preorder(Node* root) {
if (!root) return;
std::cout << root->val << " ";
preorder(root->left);
preorder(root->right);
}
int main() {
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->right->left = new Node(4);
root->right->right = new Node(5);
preorder(root);
return 0;
}Attempts:
2 left
💡 Hint
Remember preorder visits root first, then left subtree, then right subtree.
✗ Incorrect
Preorder traversal visits nodes in the order: root, left subtree, right subtree. Starting at 1, then left child 2, then right subtree 3, 4, 5.
🧠 Conceptual
intermediate1:00remaining
Number of Nodes Visited in Preorder Traversal
Given a binary tree with 7 nodes, how many nodes will be visited during a 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 all 7 nodes will be visited.
❓ Predict Output
advanced2:00remaining
Output of Preorder Traversal with Null Children
What is the output of the preorder traversal of this tree?
Tree structure:
10
/ \
null 20
/ \
30 null
Tree structure:
10
/ \
null 20
/ \
30 null
DSA C++
struct Node {
int val;
Node* left;
Node* right;
Node(int x) : val(x), left(nullptr), right(nullptr) {}
};
void preorder(Node* root) {
if (!root) return;
std::cout << root->val << " ";
preorder(root->left);
preorder(root->right);
}
int main() {
Node* root = new Node(10);
root->right = new Node(20);
root->right->left = new Node(30);
preorder(root);
return 0;
}Attempts:
2 left
💡 Hint
Preorder visits root, then left, then right. Null children are skipped.
✗ Incorrect
Starting at 10, left child is null so skipped, then right child 20, then 20's left child 30.
🔧 Debug
advanced1:30remaining
Identify the Error in Preorder Traversal Code
What error will this preorder traversal code produce when run on a non-empty tree?
DSA C++
void preorder(Node* root) {
if (root == nullptr) return;
preorder(root->left);
std::cout << root->val << " ";
preorder(root->right);
}Attempts:
2 left
💡 Hint
Check the order of printing and recursive calls.
✗ Incorrect
The code prints left subtree first, then root, then right subtree, which is inorder traversal, not preorder.
🧠 Conceptual
expert1:00remaining
Preorder Traversal Output for a Skewed Tree
Consider a binary tree where every node has only a right child, forming a chain of nodes with values 1 to 5. What is the preorder traversal output?
Attempts:
2 left
💡 Hint
Preorder visits root first, then left (none), then right.
✗ Incorrect
Since each node has only a right child, preorder visits nodes in order 1 to 5.