Skip to content
Official PYQ · 2008

GATE CS 2008 Q46 — Binary Search Trees

Explain with AI

You are given the postorder traversal, PP,  of a binary search tree on the nn elements 1,2,…,n1, 2, \dots, n. You have to determine the unique binary search tree that has PP as its postorder traversal. What is the time complexity of the most efficient algorithm for doing this?

Choose one option
View worked solution

Answer: B

To reconstruct a Binary Search Tree (BST) from its postorder traversal PP of nn elements:

  1. Uniqueness: For any BST, the inorder traversal is always the sorted order of its elements. Since we are given the postorder traversal and can obtain the inorder traversal by sorting (or we already know it is 1,2,…,n1, 2, \dots, n), the BST is uniquely determinable.
  2. Efficiency:
    • A naive recursive approach that searches for the division point between the left and right subtrees takes O(n2)O(n^2) time in the worst case (e.g., for a skewed tree).
    • We can optimize this to Θ(n)\Theta(n) time by traversing the postorder array from right to left (which yields the sequence: Root→Right→Left\text{Root} \rightarrow \text{Right} \rightarrow \text{Left}) and maintaining a valid numerical range [min,max][\text{min}, \text{max}] for each node.
    • Since each element is visited and processed a constant number of times, the overall time complexity is Θ(n)\Theta(n).

Therefore, the time complexity of the most efficient algorithm is Θ(n)\Theta(n).

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

Published
Report a correction
Keep exploringMore Binary Search Trees questions