Data Structures & AlgorithmsHardProduct company OA · Infosys pattern

Keys 50, 40, 30, 45, 47 are inserted in that order into an empty AVL tree. What is the preorder traversal of the final tree?

  1. A. 45, 40, 30, 50, 47
  2. B. 40, 30, 45, 50, 47
  3. C. 47, 40, 30, 45, 50
  4. D. 40, 30, 47, 45, 50

Correct answer: D. 40, 30, 47, 45, 50

50, 40, 30 is an LL case → right rotation at 50 → root 40 with children 30 and 50. 45 goes to 50's left. 47 goes to 45's right, making 50 unbalanced (+2) with an LR shape (left child 45, then right 47): rotate left at 45, then right at 50 → 47 becomes the subtree root with 45 and 50. Preorder 40, 30, 47, 45, 50.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic