-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNo_of_Balanced_Bt_Using_DP.java
More file actions
61 lines (43 loc) · 1.24 KB
/
No_of_Balanced_Bt_Using_DP.java
File metadata and controls
61 lines (43 loc) · 1.24 KB
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
60
61
Code: Number of Balanced BTs Using DP
Send Feedback
Given an integer h, find the possible number of balanced binary trees of height h. You just need to return the count of possible binary trees which are balanced.
This number can be huge, so return output modulus 10^9 + 7.
Time complexity should be O(h).
Input Format :
Integer h
Output Format :
Count % 10^9 + 7
Input Constraints :
1 <= h <= 10^7
Sample Input 1:
3
Sample Output 1:
15
Sample Input 2:
4
Sample Output 2:
315
public class Solution {
public static int balancedTreesOfHeightH(int h) {
int mod = (int) Math.pow(10,9)+ 7;
return balancedTreesOfHeightH(h, mod);
}
public static int balancedTreesOfHeightH(int h, int mod) {
int dp[] = new int[h+1];
dp[0] = 1;
dp[1] = 3;
if(h==0 || h==1){
return dp[h];
}
for(int i = 2; i<h;i++){
int x = dp[i-1];
int y = dp[i-2];
long res1 = (long)x * x;
long res2 = (long) x * y *2;
int modOfX = (int) (res1 % mod);
int modOfY = (int)(res2 % mod);
dp[i] = (modOfX + modOfY) % mod;
}
return dp[h-1];
}
}