Skip to content
Official PYQ · 2020

GATE CS 2020 Q5 — Binary Search Trees

Explain with AI

The preorder traversal of a binary search tree is 15,10,12,11,20,18,16,1915, 10, 12, 11, 20, 18, 16, 19. Which one of the following is the postorder traversal of the tree?

Choose one option
View worked solution

Answer: B

For any Binary Search Tree (BST), the inorder traversal is always the sorted sequence of its elements in ascending order.

Given the preorder traversal:
Preorder: 15,10,12,11,20,18,16,1915, 10, 12, 11, 20, 18, 16, 19

Sorting these elements gives the inorder traversal:
Inorder: 10,11,12,15,16,18,19,2010, 11, 12, 15, 16, 18, 19, 20

Step 1: Reconstructing the BST

  • The first element in the preorder traversal is the root: 1515.
  • Elements smaller than 1515 ({10,11,12}\{10, 11, 12\}) form the left subtree.
  • Elements larger than 1515 ({16,18,19,20}\{16, 18, 19, 20\}) form the right subtree.

By recursively applying this logic, we construct the unique BST:

       15
      /  \
    10    20
     \    /
     12  18
     /  /  \
    11 16  19

Step 2: Finding the Postorder Traversal

The postorder traversal visits nodes in the order: Left →\rightarrow Right →\rightarrow Root.

  • Postorder of Left Subtree (rooted at 1010):
    • Left of 1010: ∅\emptyset
    • Right of 1010 (rooted at 1212): Left child is 1111, so postorder is 11,1211, 12.
    • Root: 1010
    • Result: 11,12,1011, 12, 10
  • Postorder of Right Subtree (rooted at 2020):
    • Left of 2020 (rooted at 1818): Left child is 1616, right child is 1919, so postorder is 16,19,1816, 19, 18.
    • Right of 2020: ∅\emptyset
    • Root: 2020
    • Result: 16,19,18,2016, 19, 18, 20
  • Combine with Root (1515):
    Postorder=[11,12,10]+[16,19,18,20]+[15]=11,12,10,16,19,18,20,15\text{Postorder} = [11, 12, 10] + [16, 19, 18, 20] + [15] = 11, 12, 10, 16, 19, 18, 20, 15

This matches Option B.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Binary Search Trees questions