GATE Computer ScienceVery hardGATE CS (PYQ pattern)

How many distinct binary trees (left and right children are distinguished) on nodes 1..5 have preorder 1 2 3 4 5 and postorder 5 4 3 2 1?

  1. A. 16
  2. B. 1
  3. C. 14
  4. D. 5

Correct answer: A. 16

Preorder 12345 with postorder 54321 forces each node to have exactly one child (a chain). Each of the 4 non-root nodes is a left or a right child independently: 2^4 = 16 trees. Preorder + postorder cannot tell left from right for a lone child.

More Programming and Data Structures (GATE) questions

Exams that ask this topic