edited by
3,023 views
1 votes
1 votes

Consider the vales of bst $11,22,33,44,55,66,77,88$.

Which of the following is a valid sequence of preorder traversal?

  1. $55,33,11,22,44,77,66,88$
  2. $55,33,22,44,11,66,77,88$
  3. $55,33,11,44,22,66,77,88$
  4. $55,33,11,44,22,66,77,88$

Given answer is option A

But how?

edited by

2 Answers

Best answer
3 votes
3 votes

By Seeing Options, I think you have made mistake in Typing the Question, It is 33 and 55 instead of 333, 555. So, I give answer according to the Options i.e. considering 33 and 55

For BST, In-order traversal is always a Sorted Order (Ascending or Descending.. But people mostly construct BST in such a way that the In-order comes in Ascending Order.. Same case in this Question) And Since Only In-order is given (Though only key values are given but we can write In-order from the key values by Sorting them), We can not construct a Unique Pre-Order. So, We will have to adopt eliminate Options approach..

Now, Go option by Option. You can either construct BST for each option (But that would be Time consuming in the exam) But with Practice you can do it very fast, just by scanning the sequence.

I'll show you how to do it with just scanning the Sequence.

We know that Pre-order keeps on giving Roots when scanned from Left to Right.

Let's take Option B. If we consider Option B as Pre-order of the BST then 55 will be the root. So, Less than 55 will be in the Left Subtree, Hence they will occupy the next 4 slots in the Pre-oder. Now, Scanning the next element in Option-B, We have 33, So, It means 33 is the root of the Left Subtree, So, Now we can say that less than 33 will be in the Left subtree of 33, hence they will occupy the next 2 Slots. i.e. 11 and 22 (not necessarily in this Order) should be the next Two elements of the Pre-order. But In  Option B we have 44 in place of 11. So, It (Option B) can not be the Pre-order.

Similarly, Repeat the same idea for other Options. A is indeed correct.


Until you get comfortable with this Scanning the sequence Approach, I suggest you construct the BST for each Option and see. You will get the intuition behind the Scanning approach and will be able to solve such Questions very fast.

selected by
0 votes
0 votes

I think given answer is wrong.

Preorder must be 

Related questions

0 votes
0 votes
1 answer
2
4 votes
4 votes
1 answer
3
ramakrushna asked Dec 31, 2021
1,370 views
The number of insertion sequences of the numbers {1,2,3,4,5,6,7} which would lead to the following BSTHow to tackle this kind of problem. Anyone!