Ways to color a skewed tree such that parent and child have different colors | fast_way is recursive method to calculate power ; Driver code
function fastPow ( N , K ) { if ( K == 0 ) return 1 ; let temp = fastPow ( N , Math . floor ( K / 2 ) ) ; if ( K % 2 == 0 ) return temp * temp ; else return N * temp * temp ; } function countWays ( N , K ) { return K * fastPow ( K - 1 , N - 1 ) ; } let N = 3 , K = 3 ; document . write ( countWays ( N , K ) ) ;