Count unique BSTs.

Hard
0/120
Average time to solve is 15m
profile
Contributed by
18 upvotes
Asked in company
Sprinklr

Problem statement

You have been given a number ‘NUM’. Your task is to count total structurally unique binary search trees from keys 1 to ‘NUM’ considering we should use each key from 1 to ‘NUM’ only once.

Two binary search trees are different if there is at least one node during traversal which is different in values or present in one tree but not present in another tree.

Note:
The answer can be large, hence the return answer % ‘MOD’, where ‘MOD’ is a large prime number (10^9 + 7).
Detailed explanation ( Input/output format, Notes, Images )
Input format :
The first line contains a single integer ‘T’ representing the number of test cases.

The first line of each test case will contain a single integer ‘NUM’ where ‘NUM’ represents the number of keys.
Output Format :
For each test case, print a single line containing the count of total structurally unique binary search trees.

The output of each test case will be printed in a separate line.
Note:
You don’t need to print anything; It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 100   
1 <= NUM <= 500

Time limit: 1 sec
Sample Input 1:
2
2
1
Sample Output 1:
2
1
Explanation of sample input 1:
In the first test case, 2 unique BST are possible.

Example

In the second test case, only 1 tree is possible.

Example

Sample Input 2:
2
3
4
Sample Output 2:
5
14
Explanation of sample input 2:
In the first test case, 5 unique BST are possible.

Example

In the second test case, similar to the above way, 14 unique BST are possible.
Hint

Can you solve the problem recursively?

Approaches (3)
Recursion

We can assume a function for calculating the number of ways to calculate the number of trees for the given number of nodes for a fixed root. So we could calculate the number of ways to make a tree as follows. Let’s call the function F(‘X’) giving the number of trees given ‘X’ number of nodes.

 

We could calculate F(‘X’) = sum of { F(‘i’) * F(‘X’-1-’i’) }. Where i is the assumed number of nodes in the left child of the current root and so the number of remaining nodes for the right child is equal to (‘X’-1-i), hence we could add all these ways multiplicatively, so we could get the total number of ways. 

 

This value of this function is also defined as Catalan number, which has a specified formula which is equal to ((2*NUM) C (NUM) / (NUM+1)) , but we will calculate the value recursively.

 

Steps for calculating assuming ‘NUM’ number of nodes in the given tree are as follows: 

 

  1. If ‘NUM’ is equal to 0 or 1, return the answer as 1, as there is only one tree.
  2. Declare variable ‘SUM’ and initialize it with 0, where sum will store the count of total structurally unique binary search trees.
  3. Run a loop for ‘i’ = ‘0’ to ‘NUM’-1:
    1. ‘SUM’ = ‘SUM’ +  F(‘i’) * F(‘NUM’-1-’i’) to the answer.
  4. Finally, return ‘SUM’ as the answer.
Time Complexity

O(3 ^ NUM), where ‘NUM’ is the number of distinct keys.

 

The complexity estimation for above recursion can be done as follows:

 

Let's call the estimation function T(‘NUM’) for ‘NUM’ number of nodes , here it represents the number of recursive calls for ‘NUM’.

  • A call for N takes exactly 2 * (NUM - 1) recursive calls, each of them adding their own costs , 2 * ( T(1) + T(2) + … + T(NUM-1)).
  • A call for N + 1 takes exactly 2 * N recursive calls. Each of them adding their own cost, 2 * ( T(1) + T(2) + … + T(NUM-1) + T(NUM)).
  • T(NUM+1) - T(NUM) = 2 + 2 * T(NUM)
  • T(NUM) = 3 * T (NUM-1) + 2
  • T(NUM) = 3 ^ NUM

 

And hence time complexity will be O(3 ^ NUM).

Space Complexity

O(3 ^ NUM), where ‘NUM’ is the number of distinct keys.

 

We can follow the same runtime complexity estimation as we are also creating the same amount of variables in the recursion stack memory and hence space complexity will be O(3 ^ NUM).

Code Solution
(100% EXP penalty)
Count unique BSTs.
Full screen
Console