Question
We are given a set of distinct elements and an unlabeled binary tree with nodes. In how many ways can we populate the tree with the given set so that it becomes a binary search tree?
Official answer and solution
- Official answer
- B
- From
- Official GATE 2011 answer key
Solution
Use the property that the inorder traversal of any Binary Search Tree produces a uniquely sorted sequence.
- Let be a set of distinct keys sorted in strictly increasing order .
- In any valid Binary Search Tree (BST), an inorder traversal visits all nodes in strictly increasing order of their key values.
- For a given, fixed unlabeled binary tree structure with nodes, the inorder traversal sequence of tree nodes is fixed and unique.
- Therefore, the -th node visited during the inorder traversal must receive the -th sorted element for every . There is exactly 1 unique valid assignment.
Final answer
B
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2011 Computer Science and Information Technology (CS)
- Question
- 29 of 65, printed as CS Q.29
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.