Data Structures & AlgorithmsModerateTCS NQT · Infosys pattern

Keys 40, 20, 60, 10, 30, 50, 70, 25 are inserted into an empty BST. Then 20 is deleted (a node with two children is replaced by its inorder successor). What is the preorder traversal?

  1. A. 40, 30, 10, 25, 60, 50, 70
  2. B. 40, 10, 30, 25, 60, 50, 70
  3. C. 40, 25, 30, 10, 60, 50, 70
  4. D. 40, 25, 10, 30, 60, 50, 70

Correct answer: D. 40, 25, 10, 30, 60, 50, 70

20 has two children, so it is replaced by its inorder successor – the smallest key in its right subtree (30 → its left child 25). 25 moves up and its old position becomes empty. Preorder: 40, 25, 10, 30, 60, 50, 70. Using 30 (the right child) instead of the true successor gives 40, 30, 10, 25, 60, 50, 70.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic