Skip to content
Official PYQ · 2013

GATE CS 2013 Q43 — Binary Search Trees

Explain with AI

The preorder traversal sequence of a binary search tree is 30,20,10,15,25,23,39,35,4230, 20, 10, 15, 25, 23, 39, 35, 42. Which one of the following is the postorder traversal sequence of the same tree?

Choose one option
View worked solution

Answer: D

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

Given the preorder traversal:
Preorder: 30,20,10,15,25,23,39,35,4230, 20, 10, 15, 25, 23, 39, 35, 42

Sorting these keys gives the inorder traversal:
Inorder: 10,15,20,23,25,30,35,39,4210, 15, 20, 23, 25, 30, 35, 39, 42

Step 1: Reconstructing the BST

  1. The first element in the preorder sequence is the root: 3030.
  2. Elements smaller than 3030 (10,15,20,23,2510, 15, 20, 23, 25) form the left subtree. Elements larger than 3030 (35,39,4235, 39, 42) form the right subtree.
  3. For the left subtree, the next element in the preorder sequence is 2020, which becomes the root of this subtree.
    • Keys smaller than 2020 (10,1510, 15) go to its left.
    • Keys larger than 2020 (23,2523, 25) go to its right.
  4. Continuing this process recursively, we construct the unique BST:
       30
      /  \
    20    39
   /  \   / \
  10  25 35 42
   \  /
   15 23

Step 2: Finding the Postorder Traversal

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

  • Left Subtree of 3030:
    • Postorder of left child 2020:
      • Left child of 2020 (rooted at 1010): 15,1015, 10
      • Right child of 2020 (rooted at 2525): 23,2523, 25
      • Root: 2020
      This gives: 15,10,23,25,2015, 10, 23, 25, 20
  • Right Subtree of 3030:
    • Postorder of right child 3939:
      • Left child: 3535
      • Right child: 4242
      • Root: 3939
      This gives: 35,42,3935, 42, 39
  • Root Node: 3030

Combining these, the complete postorder traversal is:
15,10,23,25,20,35,42,39,3015, 10, 23, 25, 20, 35, 42, 39, 30

This matches Option D.

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

Published
Report a correction
Keep exploringMore Binary Search Trees questions