Data Structures & AlgorithmsVery hardProduct company OA

Keys 1 to 7 are inserted in increasing order into an empty AVL tree, then 4 is deleted (a node with two children is replaced by its inorder successor) and then 5 is deleted. What is the preorder traversal of the final tree?

  1. A. 6, 2, 1, 3, 5, 7
  2. B. 6, 2, 1, 3, 7
  3. C. 3, 2, 1, 6, 7
  4. D. 2, 1, 6, 3, 7

Correct answer: B. 6, 2, 1, 3, 7

Inserting 1–7 in order gives the perfect tree 4 (2 (1, 3), 6 (5, 7)). Deleting 4 replaces it with its successor 5, leaving 5 (2 (1, 3), 6 (–, 7)). Deleting the root 5 now uses successor 6: 6 (2 (1, 3), 7). Heights are 2 and 1, still balanced, so no rotation: preorder 6, 2, 1, 3, 7.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic