Possible number of Trees having N vertex | JavaScript program for the above approach ; Function to count the total number of trees possible ; Find the max element in the given array ; Level array store the number of nodes on level i initially all values are zero ; In this case tree can not be created ; To store the count of trees ; Iterate until maxElement ; Calculate level [ i ] ^ level [ i + 1 ] ; Update the count of tree ; Return the final count of trees ; Driver Code ; Given array arr [ ] ; Function call
let mod = ( 1e9 + 7 ) ; function NumberOfTrees ( arr , N ) { let maxElement = Math . max ( ... arr ) ; let level = Array . from ( { length : maxElement + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { level [ arr [ i ] ] ++ ; } if ( arr [ 0 ] != 0 level [ 0 ] != 1 ) { return 0 ; } let ans = 1 ; for ( let i = 0 ; i < maxElement ; i ++ ) { for ( let j = 0 ; j < level [ i + 1 ] ; j ++ ) { ans = ( ans * level [ i ] ) % mod ; } } return ans ; } let N = 7 ; let arr = [ 0 , 3 , 2 , 1 , 2 , 2 , 1 ] ; document . write ( NumberOfTrees ( arr , N ) ) ;