Data Structures & AlgorithmsModerateGATE CS · Infosys Mysore

Distinct keys are inserted one by one, in the order 40 38 28 68 61, 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. 40 68 61 38 28
  2. B. 28 38 61 68 40
  3. C. 40 38 28 68 61
  4. D. 40 38 68 28 61

Correct answer: C. 40 38 28 68 61

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