GATE / CS ARCHIVE

SIMILAR QUESTIONS / DATA STRUCTURES

Apply sorted inorder BST invariant

5 questions solved the same way, newest first. Back to Data Structures

How these are solved

Use left<root<right recursively: distinct keys appear in sorted order in inorder; a fixed shape therefore has one sorted-inorder labeling. Different given traversals, sets and requested outputs are retained.

2 marks · MCQ
Report issue

What a BST reveals without its insertion order

Binary Search & Balanced Trees

Question 39, GATE 2024: A BST is built by inserting distinct integers from a known set V into an empty tree, with smaller keys in each left subtree. The insertion order and tree are unknown. What can always be determined from V? A inorder traversal; B root; C preorder; D postorder.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Approach: Use the BST order invariant and test the other traversals on two possible insertion sequences.

1. Derivation. An inorder traversal of a BST with distinct keys and smaller keys to the left always visits exactly the sorted set V, regardless of insertion order. The first inserted key becomes the root, and different insertion orders can change the root, preorder, and postorder. Hence only inorder is determined in general, option A. Trivial sets can coincidentally determine more, but do not establish those claims for arbitrary V.

2. Explanation and checks. For V={1,2,3}, inserting 2,1,3 gives root 2, preorder 2,1,3 and postorder 1,3,2. Inserting 1,2,3 gives root 1, preorder 1,2,3 and postorder 3,2,1. Both have inorder 1,2,3. Recursively, inorder visits smaller values, then the node, then larger values; thus it is the sorted list for every valid shape. The examples demonstrate why knowledge of the set alone cannot recover the shape-sensitive alternatives.

Original paper, page 26 · Answer key

1 mark · MCQ
Report issue

Binary Search & Balanced Trees

Question 17, GATE 2015: Which sequences can be inorder traversals of BSTs? I 3,5,7,8,15,19,25; II 5,8,9,12,10,15,25; III 2,7,10,8,14,16,20; IV 4,6,7,9,18,20,25. A I and IV; B II and III; C II and IV; D II only.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

For distinct keys, recursively visiting left subtree, root, then right subtree produces strictly increasing order. I and IV meet this necessary condition and can be realized, for example, by inserting their keys in ascending order into right chains. II descends from 12 to 10, and III from 10 to 8, violating the property.

Solution: Inorder traversal of a BST with distinct keys must be increasing. I and IV are increasing. II places 12 before 10, and III places 10 before 8. Therefore I and IV, A.

Original paper, page 8 · Answer key

2 marks · MCQ
Report issue

Unique BST labeling for a fixed shape

Binary Search & Balanced Trees

Question 29, GATE 2011: Given n distinct elements and a fixed unlabeled binary tree with n nodes, in how many ways can the elements populate that tree so it becomes a binary search tree?

(A) 0
(B) 1
(C) n!
(D) binomial(2n,n)/(n+1)
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Inorder traversal of a BST with distinct keys must list them in increasing order. The fixed ordered tree shape has a fixed inorder sequence of node positions, so assign the smallest key to the first position, and so on. This gives exactly one valid labeling, B; Catalan numbers count alternative shapes, not labelings of this supplied shape.

This assumes the usual ordered binary tree, in which left and right child positions are fixed. Inorder visits the entire left subtree, then the node, then the entire right subtree. At each node the BST condition requires every left key to be smaller and every right key larger. Therefore assigning sorted keys along inorder both constructs a valid solution and proves uniqueness. There is at least one solution for any such shape, which rules out zero as well.

Original paper, page 8 · Answer key

2 marks · MCQ
Report issue

Inorder traversal of a binary search tree

Binary Search & Balanced Trees

Question 33, GATE 2005: BST postorder is 10, 9, 23, 22, 27, 25, 15, 50, 95, 60, 40, 29. Candidate inorder A9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95; B9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29; C29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95; D95, 50, 60, 40, 27, 23, 22, 25, 10, 9, 15, 29.
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Inorder traversal of a BST with distinct keys lists them in increasing order. Sorting the displayed key set gives 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95, option A. No full reconstruction from postorder is necessary.

For distinct keys in a BST, inorder visits every smaller key before the root and every larger key afterward, recursively producing the sorted key list. Sorting the supplied set gives A. There is no need to reconstruct the full shape from postorder unless the task asks for structure; postorder here merely specifies which keys are present.

Original paper, page 4

1 mark · MCQ
Report issue

Inorder traversal of inserted keys

Binary Search & Balanced Trees

Question 19, GATE 2003: Insert 7,5,1,8,3,6,0,9,4,2, in this order, into an initially empty binary search tree using ordinary numerical order. What is its inorder traversal? A. 7,5,1,0,3,2,4,6,8,9; B. 0,2,4,3,1,6,5,9,8,7; C. 0,1,2,3,4,5,6,7,8,9; D. 9,8,6,4,2,3,0,1,5,7.
Question 19, GATE 2003: Insert 7,5,1,8,3,6,0,9,4,2, in this order, into an initially empty binary search tree using ordinary numerical order. What is its inorder traversal? A. 7,5,1,0,3,2,4,6,8,9; B. 0,2,4,3,1,6,5,9,8,7; C. 0,1,2,3,4,5,6,7,8,9; D. 9,8,6,4,2,3,0,1,5,7.
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Step through it (23 steps)

1. At each node, all left-subtree keys are smaller and all right-subtree keys larger. Inorder recursively visits left subtree, root, right subtree, so this invariant sorts the output. The insertion order determines tree shape but cannot change this ordering property.

2. An inorder traversal of a BST visits its distinct keys in increasing order, independently of insertion order. The given keys are exactly 0 through 9, so the sequence is 0,1,2,3,4,5,6,7,8,9, C.

Original paper, page 2