Question
The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one of the following is the postorder traversal sequence of the same tree?
Official answer and solution
- Official answer
- D
- From
- Official GATE 2013 answer key
Solution
Reconstruct the unique Binary Search Tree from the given preorder traversal and then determine its postorder traversal.
The preorder traversal sequence is:
In a Binary Search Tree (BST), the inorder traversal is strictly ascending:
We can reconstruct the BST:
- The first element of preorder is the root: 30.
- Elements less than 30 form the left subtree: preorder .
- Root of left subtree is 20.
- Left of 20: . Root is 10, with right child 15.
- Right of 20: . Root is 25, with left child 23.
- Elements greater than 30 form the right subtree: preorder .
- Root of right subtree is 39.
- Left child of 39 is 35.
- Right child of 39 is 42.
Now compute the postorder traversal (Left, Right, Root):
- Left subtree of 10: empty. Right subtree: 15. Root: 10 .
- Left subtree of 25: 23. Right subtree: empty. Root: 25 .
- Subtree rooted at 20: Left is , Right is , Root is 20 .
- Subtree rooted at 39: Left is 35, Right is 42, Root is 39 .
- Whole tree: Left is , Right is , Root is 30.
Combining these gives the postorder traversal:
This matches option (D).
D
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2013 Computer Science and Information Technology (CS)
- Question
- 43 of 65, printed as CS Q.43
- 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.