C. It visits right subtree before left subtree, reversing inorder order
D. It does not print the root value
Solution
Step 1: Analyze the traversal order in the code
The code visits root.Right first, then root, then root.Left, which is reverse of inorder.
Step 2: Identify the correct inorder order
Inorder should visit left subtree first, then root, then right subtree.
Final Answer:
It visits right subtree before left subtree, reversing inorder order -> Option C
Quick Check:
Inorder = Left, Root, Right [OK]
Hint: Inorder must visit left subtree before right [OK]
Common Mistakes:
Swapping left and right subtree calls
Forgetting to check for null root
Not printing root value
5. You have a binary tree where each node contains an integer. You want to collect all node values in a list using inorder traversal. Which C# code snippet correctly implements this?
hard
A. void Inorder(Node root, List<int> result) {
if (root == null) return;
result.Add(root.Value);
Inorder(root.Left, result);
Inorder(root.Right, result);
}
B. void Inorder(Node root, List<int> result) {
if (root == null) return;
Inorder(root.Left, result);
result.Add(root.Value);
Inorder(root.Right, result);
}
C. void Inorder(Node root, List<int> result) {
if (root == null) return;
Inorder(root.Right, result);
result.Add(root.Value);
Inorder(root.Left, result);
}
D. void Inorder(Node root, List<int> result) {
if (root == null) return;
Inorder(root.Left, result);
Inorder(root.Right, result);
result.Add(root.Value);
}
Solution
Step 1: Recall inorder traversal order
Inorder visits left subtree, then root, then right subtree.
Step 2: Check which code matches this order
void Inorder(Node root, List<int> result) {
if (root == null) return;
Inorder(root.Left, result);
result.Add(root.Value);
Inorder(root.Right, result);
} visits left subtree, adds root value, then visits right subtree, correctly collecting values in order.
Final Answer:
void Inorder(Node root, List<int> result) { if (root == null) return; Inorder(root.Left, result); result.Add(root.Value); Inorder(root.Right, result); } -> Option B
Quick Check:
Left, Root, Right order with result.Add(root.Value) in middle [OK]
Hint: Add root value between left and right recursive calls [OK]
Common Mistakes:
Adding root value before left subtree
Adding root value after right subtree
Swapping left and right subtree calls
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.