-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniqueBST's.java
59 lines (52 loc) · 1.01 KB
/
UniqueBST's.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//Initial Template for Java
import java.util.*;
import java.io.*;
import java.lang.*;
// } Driver Code Ends
//User function Template for Java
class Solution
{
static long dp[];
static long driver(int n){
if(dp[n]!=0){
return dp[n];
}
long sum=0;
for(int i=1;i<=n;i++){
sum+=((driver(i-1))*(driver(n-i)))%1000000007;
}
dp[n]=sum%1000000007;
return dp[n];
}
static int numTrees(int n)
{
dp=new long[n+1];
Arrays.fill(dp,0);
if(n==0 || n==1){
return 1;
}
else{
dp[0]=1;
dp[1]=1;
int sum=0;
return (int)driver(n);
//return sum;
}
}
}
// { Driver Code Starts.
class GFG
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0)
{
int n =sc.nextInt();
Solution T = new Solution();
System.out.println(T.numTrees(n));
}
}
}
// } Driver Code Ends