问道面试题:实现pow(double x, int n),该如何处理

问道面试题:实现pow(double x, int n)
leetcode上的题目,下面是我代码

class Solution {
public:
    double pow(double x, int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(n == 0)
            return 1;
        if(x == 0)
            return 0;
        if(n > 0)
            return x*pow(x, n - 1);
        if(n < 0)
            return 1 / pow(x, -n);
    }
};

为什么提示runtime error?我知道这个算法效率比较低,但是超时了应该是“Time Limit Exceeded”这个提示啊。。请问这么写还有什么错误么。。?
------解决思路----------------------
不会吧,都一样的为什么你的不过呀
引用:
Quote: 引用:

递归是要销毁堆栈的, 当栈用完了, 就 runtime error 了.


class Solution {
public:
    double pow(double x, int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(n == 0)
            return 1;
        if(x == 0)
            return 0;
        if(n < 0)
            return 1.0 / pow(x, -n);
        if(n > 0)
        {
            double a = pow(x, n/2);
            if(n % 2 == 0)
            {
                return a*a;
            }
            else
                return a*a*x;
        }
    }
};

显示超时。
但是,别人的代码,
class Solution {  
//divide-and-conquer   
//classic   
public:  
    double pow(double x, int n) {  
        if (n == 0) return 1.0;  
        // Compute x^{n/2} and store the result into a temporary   
        // variable to avoid unnecessary computing   
        double half = pow(x, n / 2);  
        if (n % 2 == 0)  
            return half * half;  
        else if (n > 0)  
            return half * half * x;  
        else  
            return half * half / x;  
    }  
}; 

一个思路,人家的就能跑过。。这是为什么啊。。求助啊。。多谢!