Number of Binary Search Trees of height H consisting of H + 1 nodes | Java program for the above approach ; Function to calculate x ^ y modulo 1000000007 in O ( log y ) ; Stores the value of x ^ y ; Update x if it exceeds mod ; If x is divisible by mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the value of x ^ y ; Function to count the number of of BSTs of height H consisting of ( H + 1 ) nodes ; Driver code
class GFG { static int mod = 1000000007 ; static long power ( long x , int y ) { long res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static long CountBST ( int H ) { return power ( 2 , H ) ; } public static void main ( String [ ] args ) { int H = 2 ; System . out . print ( CountBST ( H ) ) ; } }