You are given the postorder traversal, , of a binary search tree on the elements . You have to determine the unique binary search tree that has as its postorder traversal. What is the time complexity of the most efficient algorithm for doing this?
View worked solution
Answer: B
To reconstruct a Binary Search Tree (BST) from its postorder traversal of elements:
- 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 ), the BST is uniquely determinable.
- Efficiency:
- A naive recursive approach that searches for the division point between the left and right subtrees takes time in the worst case (e.g., for a skewed tree).
- We can optimize this to time by traversing the postorder array from right to left (which yields the sequence: ) and maintaining a valid numerical range for each node.
- Since each element is visited and processed a constant number of times, the overall time complexity is .
Therefore, the time complexity of the most efficient algorithm is .
Free practice · Your answers on this page are not saved.
