The Gateway to Computer Science Excellence
+29 votes

We are given a set of $n$ distinct elements and an unlabeled binary tree with $n$ nodes. In how many ways can we populate the tree with the given set so that it becomes a binary search tree?

  1. $0$
  2. $1$
  3. $n!$
  4. $\frac{1} {n+1} .^{2n}C_n$
in DS by Veteran (105k points)
recategorized by | 6.7k views

what about this?This is the same structure and can be populated in two different ways.

for 15 nodes BST can be populated in 8 ways.

The 2nd pic is not a your counting process is wrong.
in 2nd pic.- Every element of right subtree should be greater than 5 but here 4 is present so it is wrong.
he said an unlabeled binary means out of all possible unlabel binary tree we took on of them then they ask how many way u form bst. so only one way
if it is given a labeled binary tree with n nodes then???

@Manish Kumar Tiwari Labeled binary tree implies that the elements are already fixed. You can't change their arrangement. So only 1 possibility.

8 Answers

+50 votes
Best answer

With $n$ nodes, there are $\frac{^{2n}Cn}{(n+1)}$ distinct tree structures possible.

Corresponding to each structure, only one binary search tree (BST) can be formed because inorder is fixed.

Here, we are already given one such structure therefore only one tree possible.

If binary trees would have been asked, $n!$ trees would have been possible corresponding to each distinct tree structure. Here, tree structure is fixed and hence, we can have only one possibility for BST as elements are distinct. For general cases: 


Correct Answer: $B$

by Loyal (8.2k points)
edited by
This is the correct explanation.

the word structure is nowhere mentioned in the question. they have used the word set. 

word set is given means it contains some  value so there is only 1 way to put the value into the node(means populate the tree with the given set)  as already structure is fixed for BST.
Structure means the unlabeled binary tree.
@sikha if binary trees would have been asked how it is n! It could be more than it..

This could be true for a complete binary tree or almost CBT
Sir as you say that inorder is given

can you please tell me how.
+28 votes
Given binary tree is unlabeled . So as it is given we are not allowed to change the formation of tree. Then To make it BST we can use atmost 1 way . As for particular structure we can not use n! arrangement of nodes (Becasue they are labeled and it is BST not BT)
by Active (1.5k points)
Yes. Here tree structure is fixed and hence we can have only 1 possibility for BST as elements are distinct. For general cases:
So for binary trees, the answer would have been n! ?
what is think about option D
@Arjun where they said structure is fixed? They did,'t use the word structure. They have just given the set.

In the question they have asked "In how many ways can we populate the tree with the given set ...". Since it is particularly mentioned "populate the tree", hence we have to set values to that structure(given unlabeled binary tree) only and it can be done by 1 way only so, that given unlabeled binary tree turns into a BST.

+8 votes

With n nodes there are 2nCn/(n+1) different structure. One structure out of these 2nCn/(n+1) structures is given.

Now no of ways to fill numbers in given structure to ensure BST property is one and only one.

by Veteran (60.5k points)
In BST left nodes have value less than root and right nodes have value greater than root. SO we could have more than one BST if we take different node for root there will be more than one BST right!!!
Take any structure and try to fill that particular structure with these n distinct keys. There will be one and only one sequence possible for a particular structure.
+5 votes
Take any structure formed by n unlabeled binary tree ... suppose n=3 ... then try to label it which will be n! (here 3!) ... then U will find only one structure satisfying the conditions of BST .. So B is the answer ...
by Boss (11.9k points)

Logic of BST

+4 votes
Just the simple logic is that we have 2nCn/(n+1) unlabelled trees , now each unlabelled tree will always correspond to one BST , consider 1,2,3 now say you draw a right skewed unlabelled tree , now u can label it only in one way 1 2 3 to form a BST , if say u draw a left skewed unlabelled tree , then u can label it as 3 2 1 , to form BST therefore each unlabelled tree gives rise to only 1 BST.


But in the question it is already given that we are provided with one unlabelled tree therefore there is only 1 way in which we can populate it to form a BST therefore answer is option A .
by Loyal (6.3k points)
In the question, the tree is also given.
Thankss sir for correction I have edited my answer .
+2 votes



by (169 points)
good approach !
0 votes

i found all the unlabeled trees can be binary search trees. Please check this and correct if iam wrong.Guys, please check whether it is correct or not.

by (21 points)

@Sangeeth680 But here we are provided with one such unlabeled binary tree. So here we have the structure of the tree and we have to fill it, in order to make it a BST which can be done in only 1 way.

–3 votes
Ans is D
by Boss (10.4k points)

Structure is fixed here. So, we can have only 1 way of filling as elements are distinct.

yes as the elements are distinct so for each structure only one order is possible .
Can you please explain what is wrong in my answer ?

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
50,644 questions
56,531 answers
101,352 users