Challenge - 5 Problems
Inorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of inorder traversal?
Given the binary tree below, what is the output of an inorder traversal (Left, Root, Right)?
DSA C
/* Tree structure: 5 / \ 3 7 / \ \ 2 4 8 */ void inorderTraversal(struct Node* root) { if (root == NULL) return; inorderTraversal(root->left); printf("%d ", root->data); inorderTraversal(root->right); }
Attempts:
2 left
💡 Hint
Remember inorder traversal visits left subtree first, then root, then right subtree.
✗ Incorrect
Inorder traversal visits nodes in the order: left child, root, right child. So for the given tree, the nodes are visited as 2, 3, 4, 5, 7, 8.
❓ Predict Output
intermediateWhat is the inorder traversal output for this tree?
Consider the binary tree below. What will be printed by an inorder traversal?
DSA C
/* Tree structure: 10 / \ 6 15 \ \ 8 20 */ void inorderTraversal(struct Node* root) { if (root == NULL) return; inorderTraversal(root->left); printf("%d ", root->data); inorderTraversal(root->right); }
Attempts:
2 left
💡 Hint
Inorder means left subtree first, then root, then right subtree.
✗ Incorrect
The inorder traversal visits nodes in this order: 6 (left child), 8 (right child of 6), 10 (root), 15 (right child), 20 (right child of 15).
🔧 Debug
advancedWhat error occurs in this inorder traversal code?
Identify the error when running this inorder traversal function on a binary tree.
DSA C
void inorderTraversal(struct Node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right->left);
}Attempts:
2 left
💡 Hint
Check the recursive call on the right subtree carefully.
✗ Incorrect
The code calls inorderTraversal(root->right->left) instead of inorderTraversal(root->right). If root->right is NULL, accessing root->right->left causes a segmentation fault.
🧠 Conceptual
advancedHow many nodes are visited in inorder traversal of this tree?
Given the binary tree below, how many nodes will be visited during an inorder traversal?
DSA C
/* Tree structure:
1
/ \
2 3
/ / \
4 5 6
*/Attempts:
2 left
💡 Hint
Count all nodes in the tree since inorder visits all nodes exactly once.
✗ Incorrect
The tree has nodes 1,2,3,4,5,6 totaling 6 nodes. Inorder traversal visits all nodes once.
🚀 Application
expertWhat is the inorder traversal output after inserting 9?
Starting from the tree below, insert node with value 9 as the right child of node 8. What is the inorder traversal output after insertion?
DSA C
/* Initial Tree: 7 / \ 3 10 / \ \ 2 8 12 */ // After insertion: // Node 9 is right child of 8 void inorderTraversal(struct Node* root) { if (root == NULL) return; inorderTraversal(root->left); printf("%d ", root->data); inorderTraversal(root->right); }
Attempts:
2 left
💡 Hint
Remember inorder visits left subtree, root, then right subtree. The new node 9 is right child of 8.
✗ Incorrect
Inorder traversal visits nodes in order: 2 (left of 3), 3, 8, 9 (right of 8), 7 (root), 10, 12.
