GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Among all binary trees (left and right children distinguished) with 6 nodes whose preorder traversal is 1 2 3 4 5 6, how many have an inorder traversal that begins with 3?

  1. A. 42
  2. B. 28
  3. C. 14
  4. D. 5

Correct answer: B. 28

The inorder starts at the leftmost node, so the path from the root goes left, left: 2 is the left child of 1, 3 is the left child of 2, and 3 has no left child. The remaining nodes 4, 5, 6 (in preorder) fill three places in order: the right subtree of 3, then the right subtree of 2, then the right subtree of 1. The number of ways to split 3 preorder-consecutive nodes into three possibly empty binary trees is the coefficient of x^3 in C(x)^3 = 28 (brute force over all 132 trees agrees).

More Programming and Data Structures (GATE) questions

Exams that ask this topic