Question
The pre-order traversal of a binary search tree is given by 12, 8, 6, 2, 7, 9, 10, 16, 15, 19, 17, 20. Then the post-order traversal of this tree is:
Official answer and solution
- Official answer
- B
- From
- Official GATE 2017 answer key
Solution
Use the property that the in-order traversal of a BST is sorted order to reconstruct the binary search tree from its pre-order traversal, then read out its post-order traversal.
Given pre-order traversal: 12, 8, 6, 2, 7, 9, 10, 16, 15, 19, 17, 20.
Since this is a BST, the in-order traversal consists of these keys sorted in ascending order:
2, 6, 7, 8, 9, 10, 12, 15, 16, 17, 19, 20.
Reconstruct the BST:
- The root is 12 (first element in pre-order).
- Keys smaller than 12 form the left subtree: with root 8.
- Left of 8: with root 6, left child 2, right child 7.
- Right of 8: with root 9 and right child 10.
- Keys greater than 12 form the right subtree: with root 16.
- Left of 16: 15.
- Right of 16: with root 19, left child 17, right child 20.
Compute post-order traversal (Left, Right, Root):
- Subtree at 6: left = 2, right = 7, root = 6
2, 7, 6. - Subtree at 9: left = none, right = 10, root = 9
10, 9. - Subtree at 8: left =
2, 7, 6, right =10, 9, root = 82, 7, 6, 10, 9, 8. - Subtree at 19: left = 17, right = 20, root = 19
17, 20, 19. - Subtree at 16: left = 15, right =
17, 20, 19, root = 1615, 17, 20, 19, 16. - Root 12: left subtree followed by right subtree followed by root 12:
2, 7, 6, 10, 9, 8, 15, 17, 20, 19, 16, 12.
This matches option B.
B
Confusing post-order traversal (Left, Right, Root) with in-order traversal (Left, Root, Right).
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2017 Computer Science and Information Technology (CS) Session 2
- Question
- 36 of 65, printed as CS Q.36
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.