Skip to content
Official PYQ · 2024

GATE CS 2024 Set 2 Q29 — Binary Search Trees

Explain with AI

​​​​​​You are given a set VV of distinct integers. A binary search tree TT is created by inserting all elements of VV one by one, starting with an empty tree. The tree TT follows the convention that, at each node, all values stored in the left subtree of the node are smaller than the value stored at the node. You are not aware of the sequence in which these values were inserted into TT, and you do not have access to TT.

Which one of the following statements is TRUE?

Choose one option
View worked solution

Answer: A

The correct option is A.

A fundamental property of any Binary Search Tree (BST) is that its inorder traversal always visits the nodes in sorted (ascending) order.

Since VV is a set of distinct integers, sorting the elements of VV in ascending order uniquely determines the inorder traversal of TT, regardless of the order in which the elements were inserted to construct the tree.


Why other options are incorrect:

The root node, preorder traversal, and postorder traversal depend entirely on the shape of the tree, which in turn depends on the insertion sequence of the elements. Since the insertion sequence is unknown, these cannot be uniquely determined.

Example:

Let V={1,2,3}V = \{1, 2, 3\}.

  • Case 1: If the insertion sequence is 2,1,32, 1, 3:
    • The root is 22.
    • Preorder traversal: 2,1,32, 1, 3
    • Postorder traversal: 1,3,21, 3, 2
    • Inorder traversal: 1,2,31, 2, 3
  • Case 2: If the insertion sequence is 1,2,31, 2, 3:
    • The root is 11.
    • Preorder traversal: 1,2,31, 2, 3
    • Postorder traversal: 3,2,13, 2, 1
    • Inorder traversal: 1,2,31, 2, 3

As shown, while the root, preorder, and postorder traversals change based on the insertion sequence, the inorder traversal remains uniquely 1,2,31, 2, 3 in all cases.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Binary Search Trees questions