-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDP - 1:Number of Balanced BTs
58 lines (48 loc) · 1.54 KB
/
DP - 1:Number of Balanced BTs
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
// public class Solution
// {
// public static int balancedTreesOfHeightH(int height)
// { int mod = (int)Math.pow(10, 9) + 7;
// return balancedTreesOfHeightH(height, mod);
// }
// public static int balancedTreesOfHeightH(int height,int mod){
// if(height==0 || height==1)
// return 1;
// int x=balancedTreesOfHeightH(height-1,mod);
// int y=balancedTreesOfHeightH(height-2,mod);
// long a=(long)x*x;
// long b=(long)x*y*2;
// int resa=(int)(a%mod);
// int resb=(int)(b%mod);
// return (resa+resb)%mod;
// }
// }
public class Solution
{
public static int balancedTreesOfHeightH(int height)
{
int storage[]=new int[height+1];
for(int i=0;i<storage.length;i++)
{
storage[i]=-1;
}
int mod=(int)Math.pow(10,9)+7;
return balancedOfTreesHeightH(storage,height,mod);
}
private static int balancedOfTreesHeightH(int storage[],int height,int mod){
if(height==0 || height==1)
{
storage[height]=1;
return 1;
}
if(storage[height]!=-1)
return storage[height];
int x=balancedOfTreesHeightH(storage,height-1,mod);
int y=balancedOfTreesHeightH(storage,height-2,mod);
long value1=(long)x*x;
long value2=(long)y*x*2;
int res1=(int)(value1%mod);
int res2=(int)(value2%mod);
storage[height]=(res1+res2)%mod;
return storage[height];
}
}