The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+69 votes

When searching for the key value $60$ in a binary search tree, nodes containing the key values $10, 20, 40, 50, 70, 80, 90$ are traversed, not necessarily in the order given. How many different orders are possible in which these key values can occur on the search path from the root to the node containing the value $60$?

  1. $35$
  2. $64$
  3. $128$
  4. $5040$
asked in DS by Boss (19.1k points)
edited by | 7.7k views


here in this question if I am applying catalan number for unique BST it will give more than 35 ,but question is different here .I AM NOT ABLE TO UNDERSTAND PLS CLEAR THIS 

both different case. Here certain condition is given , which u need to check


Whats the wrong in what the asker did ?
in this link from comment : 

There he considered 90,80,70 to always come together which is not true. As the immediate next comment mentions that  10, 20, 90, 30, 40, 80, 70, 50  is also possible (30 was not there in the original question).

In 2^6 also I am not considering 90, 80 ,70 together.

Please elaborate your approach..because in the link you gave there he took 90,80,70 together



See.., 90,80,70 need not to be contiguous but must be in that order so does 10,20,40,50.

I think its satisfied with my approach , at the same time i know 35 is correct answer but i am not getting where i am going wrong. 



Hi @HeadShot,
I think i know why 64 is incorrect for this question. Kindly check how i am trying to disprove 64. Hope i am able to convince. Thanks


Yes , thank you. Its convincing.

5 Answers

+86 votes
Best answer
$10, 20, 40, 50, 70,80, 90$

In BST search we if we go from say $10$ to $40$ while searching for $60$, we will never encounter $20$. So, $10, 20, 40$ and $50$ visited, means they are visited in order. Similarly, $90, 80$ and $70$ are visited in order. So, our required answer will be

$\frac{\text{No. of possible permutations of 7 numbers}}{\text{No. of possible permutations of numbers smaller than 60} \times \text{No. of possible permutations of numbers larger than 60}}$

(Since only one permutation is valid for both the smaller set of numbers as well as larger set of numbers)

$= \frac{7!}{4!3!}$

answered by Veteran (363k points)
edited by
@Arjun Sir:plz explain the ratio part ,how do we arrive to this?
please explain it???
Need more explanation . Not able to follow
If the question ask Number of distinct BST instead of Number of orderings,then how answer will vary?
each of the above permutations leads to one bst which is distinct so 35 BST's and the above approach is similar to the number of schedules possible with n1 operations in one transaction and n2 in other   (n1+n2)!/n1!*n2! as the ordering of the operations shouldn't change :)
awesome [email protected] sir
Can anyone please explain that why we will not encounter 20 on traversing from 10 to 40 while searching for 60?
Given no's traversed are 10,20,40,50,70,80,90 if these are already traversed then how no 60 can occur in between them if it occur then we never traversed to geater than 60 coz it's already found n if they are traversed then they will be surely in same path ,n that path height will be 6 so total order should be 2^6

why cant we use the method applied in this question

This is what given solution everywhere but question can be interpreted two type first what the solution everyone giving second when coz all node are traversed n this is not necessary these are the only nodes in BST then while searching for 60 these all will be traversed ,that thay all will be in same path so height will be 6 so total order possbile will be 2^6
@Dhiraj Raj Because( we have to search for 60).When we are at 10(we need to search 60), we'll go to an element greater than 10,say 40. When you are 40, to search for 60 you will move to element greater than 40.Moving to 20 after 40 is not correct
10, 20, 40,50 and 90,80,70 will always come in this order.

Ex. 10 20 40 50 90 80 70{S,S,S,S,L,L,L} or 10 90 20 40 80 70 50 {S,L,S,S,L,L,S}.

So we have got 2 groups of 3 and  4 numbers each. Say, group 1={L,L,L} containing nos. >60, group 2={S,S,S,S} cobtaining nos.<60. But group order remains same (10,20,40,50) and (90,80,70) so no need to worry about their combinations individually. So whereever you place S and L in 7 positions, S will be replaced in 10,20,40,50 order and likewise for L.

So out of 7 positions, we can place 3 larger nos. {L,L,L} in 7C3 ways=7!/4!*3!

(Which automatically includes combinations for choosing smaller 4 nos.)
+20 votes

Question is similar to this question :

We will convert Moves to Text.
It is given that During Search we have Traversed these nodes

as it can be seen that Red ones are bigger than $60$ and blue ones are smaller than $60$.

Path to node $60$ has involved those nodes. So, one of the possible solution to the problem is $$\{L, L, L, S, S, S, S\}$$ any other solution will contains these moves only. coz at a time on a node we can get directions as S(meaning $60$ is smaller) or L(meaning $60$ is larger) on comparison and since its given that those nodes were encountered it means directions were picked from that set. 

Hence, total number of possible solutions = all Permutations of that set, which is given by $\frac{7!}{4! \times 3!} = 35$

answer = option A

answered by Boss (30.8k points)
edited by
50 is not bigger than 60 and it is red so i think there must be some typing mistake
+9 votes
10 20 40 50 shoyld appear in order similarly  90 80 70 shoyld appear in order

It is similar to grid problem of combinatorics

So 7c3=35 is ans
answered by Boss (31.8k points)
couldn't get You , sorry , can you clarify it again .
+9 votes

The sequences 10, 20,40, 50 and 90, 80, 70 can then be shuffled together, as long as their sub-sequences keep intact. Thus we can have 10, 20, 40, 50, 90, 80, 70, but also 10, 20, 90, 30, 40, 80, 70, 50.

but 10, 20 ,80,50,90,40,70 its not the possible sequencm when you make BST for it will be ambiguous coz of 90.

In the final 7 positions I have to choose 3 positions for the larger numbers (and the remaining 4 for the smaller numbers). I can choose these in 7C3=35.(73) ways

answered by Boss (16k points)
Since all the keys are traversed while searching 60, we have to start with either 10 or 90 (2 possibilities for starting key).

Similarly., while traversing, 2 possibilities for every level  upto 6 levels and 1 possibility for 7th level (because there are 7 keys).

After doing this 60 can be found.

So 2^6=64 different orders possible.

Plz correct if Iam wrong @arjunsir
I also used this approach.
There is no 3 in the question,

Pls exaplain it
+9 votes

In BST search we if we go from say 10 40 while searching for 60, we will never encounter 20 So, 10,20,40 and 50 visited, means they are visited in order. Similarly, 90,80 and 70 are visited in order.

It means that suppose we have 7 place now we have to select 4 places out of 7 for first 4 elements and we have to place them so , it can be done in $^7c_4$ =$\frac{7!}{3!4!}$ways

Possible arrangements can be

_10_ _ 20 40 50

Or 10_20_ 40_50

so we have to place in such a way that  they will follow the order 10,20,30,40 now for remaining elements 90 ,80,70 there is only one choice we can place them in one way  on 3 remaining places so,it will be $\frac{7!}{3!4!}$$^3c_3$ways but they should follow the order 90,80,70 

See--It can be placed on blank places as shown above 

90 10 80 70 20 40 50

Or 10 90 20 80 40 70 50

Answer -$\frac{7}{3!4!}$$^3c_3$=35

(For better understanding You can draw a tree in the order which I had follow)

answered by Active (4.5k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

42,686 questions
48,650 answers
63,961 users