GATE Computer ScienceHardGATE CS (PYQ pattern)

Which one of the following sequences can NOT be the postorder traversal of a binary search tree with distinct keys?

  1. A. 4 7 6 11 13 12 8 17 20 15
  2. B. 5 7 6 12 10 8 18 22 20 15
  3. C. 2 6 4 11 9 7 15 19 17 13
  4. D. 3 9 5 7 12 10 16 20 18 14

Correct answer: D. 3 9 5 7 12 10 16 20 18 14

Check recursively: the last key is the root, the keys before it must be a block of smaller keys followed by a block of larger keys, and each block must itself be valid. In 3 9 5 7 12 10 16 20 18 14 the root is 14 and its left block 3 9 5 7 12 10 has root 10 with left block 3 9 5 7, whose root 7 is followed after 3 by 9 and then 5 < 7: a smaller key after a larger one, so it fails. The other three pass at every level.

More Programming and Data Structures (GATE) questions

Exams that ask this topic