GATE / CS ARCHIVE

GATE CSE 2011 / QUESTION 29

Unique BST labeling for a fixed shape

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

GATE 2011, Q29 2 marks · MCQ
Report issue

Binary Search & Balanced Trees

Question 29, GATE 2011: Given n distinct elements and a fixed unlabeled binary tree with n nodes, in how many ways can the elements populate that tree so it becomes a binary search tree?

(A) 0
(B) 1
(C) n!
(D) binomial(2n,n)/(n+1)
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Inorder traversal of a BST with distinct keys must list them in increasing order. The fixed ordered tree shape has a fixed inorder sequence of node positions, so assign the smallest key to the first position, and so on. This gives exactly one valid labeling, B; Catalan numbers count alternative shapes, not labelings of this supplied shape.

This assumes the usual ordered binary tree, in which left and right child positions are fixed. Inorder visits the entire left subtree, then the node, then the entire right subtree. At each node the BST condition requires every left key to be smaller and every right key larger. Therefore assigning sorted keys along inorder both constructs a valid solution and proves uniqueness. There is at least one solution for any such shape, which rules out zero as well.

Original paper, page 8 · Answer key