B. It visits nodes in postorder instead of inorder
C. It visits nodes in reverse inorder (Right, Root, Left)
D. It causes a NullPointerException
Solution
Step 1: Analyze the recursive call order
The method calls inorder on right subtree first, then prints root, then calls inorder on left subtree.
Step 2: Compare with inorder traversal definition
Standard inorder is Left, Root, Right. This code visits Right, Root, Left, which is reverse inorder.
Final Answer:
Visits nodes in reverse inorder (Right, Root, Left) -> Option C
Quick Check:
Right, Root, Left order detected [OK]
Hint: Check order of recursive calls to find traversal type [OK]
Common Mistakes:
Assuming it is correct inorder
Ignoring order of recursive calls
Confusing traversal types
5. You have a binary tree where each node contains a unique integer. You want to print all node values in ascending order using inorder traversal. However, the tree is not a binary search tree (BST). Which approach ensures correct ascending order output?
hard
A. Perform inorder traversal directly and print values as visited
B. Perform inorder traversal and then sort the collected values before printing
C. Perform preorder traversal and print values as visited
D. Perform postorder traversal and print values as visited
Solution
Step 1: Understand inorder traversal on non-BST
Inorder traversal visits nodes in Left, Root, Right order but does not guarantee sorted order if tree is not BST.
Step 2: Ensure ascending order output
To get ascending order, collect all node values during traversal, then sort them before printing.
Final Answer:
Perform inorder traversal and then sort the collected values before printing -> Option B
Quick Check:
Non-BST inorder not sorted; sorting needed [OK]
Hint: Inorder only sorts BST; sort after traversal for non-BST [OK]
Common Mistakes:
Assuming inorder traversal always sorts
Printing values during traversal without sorting
Using preorder or postorder expecting sorted output
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.