Data Structures & AlgorithmsVery hardGATE CS · Infosys Mysore

Distinct keys are inserted one by one, in the order 49 84 21 29 17 35 89 31 43 24, into an empty binary search tree (a smaller key goes to the left, a larger key to the right). What is the preorder traversal of the resulting tree?

  1. A. 49 84 89 21 29 35 43 31 24 17
  2. B. 17 24 31 43 35 29 21 89 84 49
  3. C. 49 21 17 29 24 35 31 43 84 89
  4. D. 49 21 84 17 29 89 24 35 31 43

Correct answer: C. 49 21 17 29 24 35 31 43 84 89

Each insertion walks down from the root by comparing with the current node, so the first key is the root. Preorder then prints root, left subtree, right subtree; the mirror-image rule of putting larger keys left is a common slip.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic