Open In App

GATE | GATE-CS-2016 (Set 2) | Question 50

Like Article
Like
Save
Share
Report

The number of ways in which the numbers 1, 2, 3, 4, 5, 6, 7 can be inserted in an empty binary search tree, such that the resulting tree has height 6, is _____________

Note: The height of a tree with a single node is 0.

[This question was originally a Fill-in-the-Blanks question]
(A) 2
(B) 4
(C) 64
(D) 32


Answer: (C)

Explanation: To get height 6, we need to put either 1 or 7 at root.

So count can be written as T(n) = 2*T(n-1) with T(1) = 1


    7
   / 
 [1..6]  

    1
      \
     [2..7] 

Therefore count is 26 = 64

Another Explanation:

Consider these cases,
1 2 3 4 5 6 7
1 2 3 4 5 7 6
1 7 6 5 4 3 2
1 7 6 5 4 2 3
7 6 5 4 3 2 1
7 6 5 4 3 1 2
7 1 2 3 4 5 6
7 1 2 3 4 6 5

For height 6, we have 2 choices. Either we select the root as 1 or 7.
Suppose we select 7.
Now, we have 6 nodes and remaining height = 5.
So, now we have 2 ways to select root for this sub-tree also.
Now, we keep on repeating the same procedure till remaining height = 1
For this last case also, we have 2 ways.

Therefore, total number of ways = 26= 64


Quiz of this Question


Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads