Challenge - 5 Problems
Inorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Inorder Traversal on a Simple Binary Tree
What is the output of the inorder traversal (Left, Root, Right) of the following binary tree?
Tree structure:
2
/ \ 1 3
Tree structure:
2
/ \ 1 3
DSA C++
struct Node {
int val;
Node* left;
Node* right;
Node(int x) : val(x), left(nullptr), right(nullptr) {}
};
void inorder(Node* root, std::vector<int>& result) {
if (!root) return;
inorder(root->left, result);
result.push_back(root->val);
inorder(root->right, result);
}
int main() {
Node* root = new Node(2);
root->left = new Node(1);
root->right = new Node(3);
std::vector<int> result;
inorder(root, result);
for (int v : result) std::cout << v << " ";
return 0;
}Attempts:
2 left
💡 Hint
Remember inorder traversal visits left child first, then root, then right child.
✗ Incorrect
Inorder traversal visits nodes in the order: left subtree, root, right subtree. For this tree, left is 1, root is 2, right is 3, so output is [1, 2, 3].
❓ Predict Output
intermediate2:00remaining
Inorder Traversal Output of a Larger Tree
What is the output of inorder traversal (Left, Root, Right) for this binary tree?
Tree structure:
4
/ \ 2 5 / \ 1 3
Tree structure:
4
/ \ 2 5 / \ 1 3
DSA C++
struct Node {
int val;
Node* left;
Node* right;
Node(int x) : val(x), left(nullptr), right(nullptr) {}
};
void inorder(Node* root, std::vector<int>& result) {
if (!root) return;
inorder(root->left, result);
result.push_back(root->val);
inorder(root->right, result);
}
int main() {
Node* root = new Node(4);
root->left = new Node(2);
root->right = new Node(5);
root->left->left = new Node(1);
root->left->right = new Node(3);
std::vector<int> result;
inorder(root, result);
for (int v : result) std::cout << v << " ";
return 0;
}Attempts:
2 left
💡 Hint
Inorder traversal visits left subtree fully before root, then right subtree.
✗ Incorrect
The inorder traversal visits nodes in order: left subtree (1,2,3), root (4), right subtree (5). So output is [1, 2, 3, 4, 5].
🔧 Debug
advanced2:00remaining
Identify the Error in Inorder Traversal Implementation
What error will this inorder traversal code cause when run on a non-empty tree?
DSA C++
void inorder(Node* root, std::vector<int>& result) { if (root == nullptr) return; inorder(root->right, result); result.push_back(root->val); inorder(root->left, result); }
Attempts:
2 left
💡 Hint
Check the order of recursive calls and when the root value is added.
✗ Incorrect
The code visits right subtree first, then root, then left subtree, which is reverse inorder traversal, not standard inorder.
🧠 Conceptual
advanced1:00remaining
Number of Nodes Visited in Inorder Traversal
Given a binary tree with 7 nodes, how many nodes will be visited during a complete inorder traversal?
Attempts:
2 left
💡 Hint
Inorder traversal visits every node exactly once.
✗ Incorrect
Inorder traversal visits every node once, so for 7 nodes, it visits 7 nodes.
❓ Predict Output
expert3:00remaining
Inorder Traversal Output of Unbalanced Tree
What is the output of inorder traversal (Left, Root, Right) for this unbalanced binary tree?
Tree structure:
10
/ \ 5 15 \ \ 7 20
Tree structure:
10
/ \ 5 15 \ \ 7 20
DSA C++
struct Node {
int val;
Node* left;
Node* right;
Node(int x) : val(x), left(nullptr), right(nullptr) {}
};
void inorder(Node* root, std::vector<int>& result) {
if (!root) return;
inorder(root->left, result);
result.push_back(root->val);
inorder(root->right, result);
}
int main() {
Node* root = new Node(10);
root->left = new Node(5);
root->right = new Node(15);
root->left->right = new Node(7);
root->right->right = new Node(20);
std::vector<int> result;
inorder(root, result);
for (int v : result) std::cout << v << " ";
return 0;
}Attempts:
2 left
💡 Hint
Follow left subtree fully, then root, then right subtree.
✗ Incorrect
Inorder traversal visits left subtree (5,7), root (10), right subtree (15,20). So output is [5, 7, 10, 15, 20].