Data Structures & AlgorithmsVery hardCodeforces Div 2 · ICPC regional

Keys are inserted in the order 12 48 72 44 38 26 into an empty binary search tree. Then the key 48 (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. 12 72 44 38 26
  2. B. 12 48 44 38 26 72
  3. C. 12 72 44 38 26 72
  4. D. 12 44 38 26 72

Correct answer: A. 12 72 44 38 26

The successor (smallest key of the right subtree) replaces 48, so the preorder becomes 12 72 44 38 26. 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