The following three are known to be the preorder, inorder and postorder…
2008
The following three are known to be the preorder, inorder and postorder sequences of a binary tree. But it is not known which is which.
MBCAFHPYK
KAMCBYPFH
MABCKYFPH
Pick the true statement from the following.
- A.
I and II are preorder and inorder sequences, respectively
- B.
I and III are preorder and postorder sequences, respectively
- C.
II is the inorder sequence, but nothing more can be said about the other two sequences
- D.
II and III are the preorder and inorder sequences, respectively
Attempted by 192 students.
Show answer & explanation
Correct answer: D
Given sequences: MBCAFHPYK ; KAMCBYPFH ; MABCKYFPH
Key observation: In a preorder traversal the root appears first; in a postorder traversal the root appears last.
Because the letter K appears first in KAMCBYPFH and last in MBCAFHPYK, K is the tree root. Therefore KAMCBYPFH is the preorder sequence, MBCAFHPYK is the postorder sequence, and the remaining sequence MABCKYFPH must be the inorder sequence. Verify by reconstructing the tree and its postorder:
Use preorder KAMCBYPFH and inorder MABCKYFPH. Root = K. Inorder splits at K into left = [M, A, B, C] and right = [Y, F, P, H]. Preorder remainder is A M C B Y P F H, which places the left-subtree nodes first and the right-subtree nodes after.
Left subtree: preorder A M C B and inorder M A B C. Root = A. Left child = M (leaf). Right subtree has inorder [B, C] and preorder [C, B], so root = C with left child = B. Left-subtree postorder = M B C A.
Right subtree: preorder Y P F H and inorder Y F P H. Root = Y. Right subtree (under Y) has preorder P F H and inorder F P H; root = P with left = F and right = H. Right-subtree postorder = F H P Y.
Combine: left postorder (M B C A) + right postorder (F H P Y) + root K gives MBCAFHPYK, which matches the first sequence.
Conclusion: KAMCBYPFH is the preorder sequence, MABCKYFPH is the inorder sequence, and MBCAFHPYK is the postorder sequence. The correct statement is that the second and third sequences are the preorder and inorder sequences, respectively.
A video solution is available for this question — log in and enroll to watch it.