We are given a set of \(n\) distinct elements and an unlabeled binary tree…
2011
We are given a set of \(n\) distinct elements and an unlabeled binary tree with \(n\) nodes. In how many ways can we populate the tree with the given set so that it becomes a binary search tree?
- A.
\(0\) - B.
\(1\) - C.
\(n!\) - D.
\(\frac{1} {n+1} .^{2n}C_n\)
Attempted by 550 students.
Show answer & explanation
Correct answer: B
Final answer: There is exactly one way to populate the given unlabeled binary tree so that it becomes a binary search tree.
Key idea: Inorder traversal of a binary search tree visits the keys in increasing order.
Sort the given n distinct elements.
Traverse the fixed tree shape in inorder and assign the sorted elements to the nodes in that order (smallest to first inorder node, next to second, and so on).
Because the BST property requires the inorder sequence to be sorted, this construction yields a valid BST and no other assignment can satisfy the inorder-sorted requirement. Hence the assignment is unique.
Notes on other counts:
The value 0 is incorrect because at least one valid labeling exists (the inorder-sorted assignment).
The value n! counts all possible bijections of elements to node positions without the BST constraint, so it overcounts.
The Catalan number (1/(n+1) * C(2n,n)) counts the number of different unlabeled binary tree shapes with n nodes, not the number of labelings of a fixed shape into a BST.
A video solution is available for this question — log in and enroll to watch it.