With 7 nodes of distinct values (unique), how many Binary search trees (BST) can be formed such that:
- Exactly $1$ leaf node(s) present?
- Exactly $2$ leaf nodes present?
I was able to solve the first one(and the actual problem was also that, but then I though what if the process can be extended)
Here's my take on the 1st one
Here the elements are $1,2,3,4,5,6,7$ (let's assume that for simplicity) and we need only $1$ leaf
So let us fix a root node: To fix the root node we have $2$choice either $1$ or $7$ from $1,2,3,4,5,6,7$
(if we select any non-extreme elements then only $1$ leaf is not possible)
Let us say we select $1$ as root node
In each and every level we have $2$ different option that is to select i,e; either of extreme elements
So in 2nd level we can select $2,3,4,5,6,7$
Let us say we select $7$ in 2nd level
We still have 2 option to select in $2,3,4,5,6$
So this is true in all levels except last level since only $1$ element is left.
Therefore, total possibilities are : $2 * 2 * 2 * 2 * 2$ = $2^5$
The same condition is true for $7$ as a root
Hence, $2^5* 2$ (1 as root or 7 as root) = $2^6$.
(Which comes out as $2^{number of nodes -1}$)
But how can we now approach the second subpart or it can be generalized for let's say (2,3,4) leaf nodes?
- Exactly 2 leaf nodes present?