GATE / CS ARCHIVE

GATE CSE 2008 / QUESTION 46

Reconstructing a BST from postorder

Data Structures · Binary Search & Balanced Trees · Full GATE CSE 2008 paper

GATE 2008, Q46 2 marks · MCQ
Report issue

Binary Search & Balanced Trees

Question 46, GATE 2008: Given the postorder traversal P of a BST on distinct elements 1,…,n, what is the most efficient time to construct the unique BST having traversal P?

(A) Θ(log n)
(B) Θ(n)
(C) Θ(n log n)
(D) None: the tree cannot be uniquely determined
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Θ(n) time is sufficient and necessary. Read the postorder sequence backward: the next element is the root, followed by the right subtree in reverse postorder and then the left.
2. A recursive builder with allowed key interval (low,high) consumes a value only if it belongs in that interval; after creating a node with key r, build the right interval (r,high), then the left (low,r). Each key is consumed once and there are O(n) calls, so the time is O(n).
3. Distinct BST keys and their ordering make the construction unique. Reading and outputting n nodes requires Ω(n), yielding Θ(n).

Original paper, page 8