GATE Computer ScienceVery hardGATE CSE

A binary tree on keys 1 to 6 has preorder 1 2 3 4 5 6 and postorder 3 2 6 5 4 1. Treating a left child and a right child as different, how many distinct binary trees have exactly these two traversals?

  1. A. 8
  2. B. 16
  3. C. 7
  4. D. 1

Correct answer: A. 8

A node with a single child can have it on either side; 1 has left subtree {2,3} and right subtree {4,5,6}; 2 has one child (2 ways), and in the chain 4–5–6 each of 4 and 5 has one child (2 ways each), so 2·4 = 8 trees.

More Programming and Data Structures (GATE) questions

Exams that ask this topic