GATE / CS ARCHIVE

SIMILAR QUESTIONS / DATA STRUCTURES

Recover BST postorder from preorder

4 questions solved the same way, newest first. Back to Data Structures

How these are solved

Split preorder after root into contiguous smaller and larger key blocks under ancestor bounds; recursively emit left,right,root and read full sequence or requested rank. 2026 asks a position, not the entire traversal.

2 marks · NAT
Report issue

Reconstructing a BST from preorder

Binary Search & Balanced Trees

Question 62, GATE 2026: A binary search tree has preorder sequence 50,25,13,40,30,47,75,60,70,80,77. Find the 1-based position of 60 in its postorder traversal.
Tap to enlarge
Answer & solution

Official answer: 7

Step through it (25 steps)

Givens and goal

A binary search tree has preorder sequence 50,25,13,40,30,47,75,60,70,80,77. Find the 1-based position of 60 in its postorder traversal.

Approach

Recursively split the preorder at each root according to BST value bounds, then traverse left, right, root.

Step-by-step solution

1. The first preorder entry is root 50. The following values below 50 form its left subtree: 25,13,40,30,47. The rest form its right subtree: 75,60,70,80,77.
2. In the left subtree, 25 has left child 13 and right subtree rooted at 40, whose children are 30 and 47. Its postorder is therefore 13,30,47,40,25.
3. In the right subtree, root 75 has a left subtree rooted at 60, containing 70 as 60’s right child. Its right subtree is rooted at 80, with left child 77. Thus that subtree’s postorder is 70,60,77,80,75.
4. The reconstructed tree is:

              50
          /        \
        25          75
       /  \        /  \
     13   40      60   80
         /  \      \  /
        30  47     70 77

5. Concatenate left-subtree postorder, right-subtree postorder and root: 13,30,47,40,25,70,60,77,80,75,50. The first five positions belong to the left subtree, position 6 is 70 and position 7 is 60.
6. Therefore the answer is 7. Preorder visits root first, while postorder visits root last; simply looking up 60’s original preorder position would answer the wrong traversal question.

Original paper, page 45 · Answer key

1 mark · MCQ
Report issue

Binary Search & Balanced Trees

Question 5, GATE 2020: A BST preorder is 15,10,12,11,20,18,16,19. Find postorder. A 10,11,12,15,16,18,19,20; B 11,12,10,16,19,18,20,15; C 20,19,18,16,15,12,11,10; D 19,16,18,20,11,12,10,15.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Step through it (19 steps)

Worked explanation

Preorder lists root, left subtree, right subtree; the BST ordering tells us where the left subtree ends. The reconstructed tree has edges 15→10(left),15→20(right),10→12(right),12→11(left),20→18(left),18→16(left),18→19(right). Postorder recursively visits left, right, root, hence the entire left subtree appears before the right subtree and 15 must be last. Option A is sorted inorder, C follows a different ordering, and D puts the right subtree before the left. This explicit edge list also makes the reconstruction checkable without a picture.

Derivation and answer

Root15 splits the preorder into left10, 12, 11 and right20, 18, 16, 19. On the left, 10 has right child12 with left child11, giving postorder 11, 12, 10. On the right, 20 has left child18 with children 16, 19, giving 16, 19, 18, 20. Append the root: 11, 12, 10, 16, 19, 18, 20, 15, B.

Original paper, page 5 · Answer key

2 marks · MCQ
Report issue

Binary Search & Balanced Trees

Question 36, GATE 2017: A binary search tree has preorder 12,8,6,2,7,9,10,16,15,19,17,20. Which is its postorder? A:2,6,7,8,9,10,12,15,16,17,19,20. B:2,7,6,10,9,8,15,17,20,19,16,12. C:7,2,6,8,9,10,20,17,19,15,16,12. D:7,6,2,10,9,8,15,16,17,20,19,12.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Step through it (27 steps)

1. Preorder visits root,left,right, so the first value 12 is the root. The contiguous values below 12 form its left preorder 8,6,2,7,9,10; the remaining values form right preorder 16,15,19,17,20.

2. Reconstruct the left side: root 8 has left root 6 with children 2 and 7, and right root 9 with right child 10. Its left-right-root postorder is 2,7,6,10,9,8.

3. On the right, root 16 has left child 15 and right root 19 with children 17 and 20. Its postorder is 15,17,20,19,16. Concatenate left postorder, right postorder and final root 12, obtaining B. Sorting the values would instead produce inorder, not postorder.

Original paper, page 14 · Answer key

2 marks · MCQ
Report issue

Recover BST postorder from preorder

Binary Search & Balanced Trees

Question 43, GATE 2013: A BST has preorder 30, 20, 10, 15, 25, 23, 39, 35, 42. Choose postorder: A10, 20, 15, 23, 25, 35, 42, 39, 30; B15, 10, 25, 23, 20, 42, 35, 39, 30; C15, 20, 10, 23, 25, 42, 35, 39, 30; D15, 10, 23, 25, 20, 35, 42, 39, 30.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Step through it (21 steps)

The root is 30. Its preorder left-subtree segment is 20, 10, 15, 25, 23 and its right-subtree segment is 39, 35, 42. Under 20, node 10 has right child 15, and node 25 has left child 23; their postorders are 15, 10 and 23, 25, giving the full left-subtree postorder 15, 10, 23, 25, 20. Under 39 are children 35 and 42, so its postorder is 35, 42, 39. Appending the root gives 15, 10, 23, 25, 20, 35, 42, 39, 30, option D.

Preorder identifies each subtree’s root first. Split the remaining values around 30: values below 30 form the left subtree and above 30 the right. Repeat around 20 and 39. Postorder reverses the placement of each root in the traversal: recursively emit its left subtree, then right subtree, then the root. This yields left block 15, 10, 23, 25, 20 and right block 35, 42, 39, followed by 30. Checking only that 30 is last is insufficient; every nested subtree must satisfy the same rule.

Original paper, page 10 · Answer key