Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div2 D

A binary search tree has 6 distinct keys and preorder traversal 52 1 3 23 48 53. How many of the 6! orders in which these keys can be inserted, one at a time, into an empty binary search tree produce exactly this tree?

  1. A. 720
  2. B. 132
  3. C. 5
  4. D. 120

Correct answer: C. 5

Insertion order must put each parent before its descendants, so the count is the number of linear extensions of the tree: n! divided by the product of all subtree sizes. The total n!, the Catalan number, and the product itself are the traps.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic