retagged by
77,389 views
256 256 votes
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$.

22 Answers

Best answer
228 228 votes
We need to fill $7$ levels with $7$ elements. So, at each level, we have exactly $2$ possible options like $1$ and $7$ for root- one corresponding to going left and the other corresponding to going right. This is the same for all levels up to 6 giving $2^6 = 64$ possible ways. In extreme cases, we can get fully left-skewed or right-skewed trees and otherwise, the shape of the tree will be some form of zigzag (every node having a single child).
edited by
239 239 votes

WELL its RECURRENCE QUESTION ,,,,but for that need to observe the pattern ..first and then THE 
RECURRENCE RELATION WILL BE N(h)=2N(h-1)

42 42 votes
A simple way to solve this question:
See that we need height of 6 and we have 7 elements at hand. So obviously any node in BST can't have two children.Now think about if root node is 2 or 3 or 4 or 5 or 6,then that root must have 2 children.So root must be either 1 or 7.Let denote root as 1st level.
So at 1st level, we either can choose 1 or 7(2 choices)
At 2nd level,similarly we can choose 2 or 6(2 choices)
At 3rd level,similarly we can choose 3 or 5(2 choices)
At 4th level,similarly we can choose 4 or 4(2 choices)
At 5th level,similarly we can choose 5 or 3(2 choices)
At 6th level,similarly we can choose 6 or 2(2 choices)
At 7th level, we will have only 1 number remaining.
So total number of BSTs possible=2^6=64
1 flag:
✌ Edit necessary (Umesh Shelke “BST have two childrens”)
26 26 votes

Let $f(n)$ be the number of the binary search trees of $(n-1)$ height (i.e. maximum height) using numbers $(x+1,x+2,x+3,...,x+n)$ where $x \in \mathrm{R}$.


For $n=1$, the answer is $1$ since there is only one node of height $0$. $\therefore f(1)=1$

For $n=2$, the answer is $2$ since there are two cases of height $1$ starting $2$ as root and $1$ as root . $\therefore f(2)=2$ 

 

For $n=3$, the answer is $4$.

 

 

Let's observe how $f(n)$ is growing from $f(n-1)$. We can add the $n^{\mathrm{th}}$ node taking as the root to the trees of $(n-1)$ nodes $(1,2,3,...,n-1)$. For this case of $(n-1)$ nodes, there are $f(n-1)$ trees. Still there are more cases to be taken. Take $1$ as the root and the rest $(n-1)$ nodes are $(2,3,...,n)$ for which there are also $f(n-1)$ trees. [ For example, we can add $3$ as the root to trees of two nodes $(1,2)$. Besides taking $1$ as the root, there can be trees using $(2,3)$.  That's why $f(3)=f(2)+f(2)=2+2=4$ ]

 

So $f(n)=\{\text{trees using }(1,2,3,...,n-1)\text{ nodes having }n^{\mathrm{th}}\text{ node as the root}\}+\{\text{trees using }(2,3,4...,n)\text{ nodes having 1 as the root}\}$

$\begin{align}\therefore f(n)&=f(n-1)+f(n-1)\\ &=2f(n-1)\\&=2^2f(n-2)\\&=2^3f(n-3)\\&=\cdots\\&=2^{n-1}f(1)\\&=2^{n-1}; ~[\because f(1)=1] \end{align}$

 

$\therefore f(7)=2^{7-1}=2^6=64$.

So the correct answer is $64$.


 

edited by
19 19 votes
there can be total 2^6 skew tree ,and being a BST there is only one permumation for a given BST .

ytotal 2^6 diffrnect skew trees are possible
5 5 votes

But  , here your resulting tree height must be 6.......

so it is only possible when your tree is either left-skew or right-skew ....

and above tree is binary search tree..........

so either you can insert in increasing order (right-skew tree) or insert in decreasing order (left-skew tree).....

so only possible way is 2....................

Emphasized point=>  (1) height must be 6      (2)  Binary search tree

think about it !!!!

Answer:
Position:
Show:

Related questions

109 109 votes
5 answers 5 answers
41.4k
41.4k views
Akash Kanase asked Feb 12, 2016
41,389 views
A complete binary min-heap is made by including each integer in $[1, 1023]$ exactly once. The depth of a node in the heap is the length of the path from the root of the h...
103 103 votes
7 answers 7 answers
27.8k
27.8k views
Akash Kanase asked Feb 12, 2016
27,830 views
Consider the following New-order strategy for traversing a binary tree:Visit the root;Visit the right subtree using New-order;Visit the left subtree using New-order;The N...
159 159 votes
14 answers 14 answers
55.8k
55.8k views
Akash Kanase asked Feb 12, 2016
55,826 views
$N$ items are stored in a sorted doubly linked list. For a delete operation, a pointer is provided to the record to be deleted. For a decrease-key operation, a pointer is...
141 141 votes
15 answers 15 answers
39.5k
39.5k views
Akash Kanase asked Feb 12, 2016
39,480 views
Suppose the functions $F$ and $G$ can be computed in $5$ and $3$ nanoseconds by functional units $U_{F}$ and $U_{G}$, respectively. Given two instances of $U_{F}$ and two...