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?
- A. 4 7 6 11 13 12 8 17 20 15
- B. 5 7 6 12 10 8 18 22 20 15
- C. 2 6 4 11 9 7 15 19 17 13
- 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.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- Keys 1 to 6 are inserted into an empty BST in each of the 720 possible orders. For how many orders is the height of the resulting tree…
- All 120 insertion orders of keys 1..5 into an empty BST are equally likely. What is the probability that the resulting tree has height…
- Keys 40, 20, 10, 25, 30, 22, 50, 5, 3, 35, 45, 60 are inserted in this order into an initially empty AVL tree (a double rotation counts as…
- Keys 1, 2, 3, ..., 20 are inserted in increasing order into an initially empty AVL tree. What is the key at the root, and how many…
- An AVL tree of height 6 (a single node has height 0) has the minimum possible number of nodes, and its keys are 1, 2, ..., n for that…
- The array [4, 10, 3, 5, 1, 8, 7, 2, 9, 6, 11, 12] is converted into a max-heap by Floyd's bottom-up build (sift-down from the last…