A binary search tree contains the values 1, 2, 3, 4, 5, 6, 7, 8. The tree is…

1997

A binary search tree contains the values 1, 2, 3, 4, 5, 6, 7, 8. The tree is traversed in pre-order and the values are printed out. Which of the following sequences is a valid output?

  1. A.

    53124786

  2. B.

    53126487

  3. C.

    53241678

  4. D.

    53124768

Attempted by 48 students.

Show answer & explanation

Correct answer: D

A valid pre-order traversal of a BST with values 1-8 must start with the root. Since it is a binary search tree, all left subtree values are less than the root and right subtree values are greater.

image.png

Explore the full course: Gate Guidance By Sanchit Sir