StackOverflow err despite using tail recursion in Pow(x,n)
569
Oct 06, 2020

class Solution {

public double myPow(double x, int n) {
    if(n == 0 || x == 1){return 1;}
    if(x == 0){return 0;}
    if(n < 0){n = n*-1;x = 1/x;}
    return help(x,n-1,x);
}
public double help(double x,int n,double nums){
    if(n == 0){return nums;}
    return help(x,n-1,nums*x);
}

}

Someone suggested to use tail recursion to resolve the StackOverflow err problem. After watching a video on the same I came up with this code , but it still doesent work!

Comments (5)