Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2

Keys are inserted in the order 32 20 62 89 57 42 59 into an empty binary search tree. Then the key 32 (which has two children) is deleted by replacing it with its INORDER SUCCESSOR, and the successor node is removed from the right subtree. What is the preorder traversal (root, left, right) of the tree afterwards? Give the keys separated by spaces.

  1. A. 32 20 62 57 42 59 89
  2. B. 20 62 57 42 59 89
  3. C. 42 20 62 57 59 89
  4. D. 42 20 62 57 42 59 89

Correct answer: C. 42 20 62 57 59 89

The successor (smallest key of the right subtree) replaces 32, so the preorder becomes 42 20 62 57 59 89. Using the predecessor instead gives a different shape, and forgetting to remove the successor node leaves a duplicate.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic