Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

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 )
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.
Full screen
Console