Why recursion doesn't work?

  • 0

    For the climbing stairs problem, I wrote my code as below:

    class Solution(object):
        def climbStairs(self,n):    
            if n <= 2:
                return n
                return climbStairs(n-1)+climbStairs(n-2)

    The code works well on Python 3, but when I test it on Leetcode (e.g., n=3 or larger), it shows Runtime error:

    Line 6: NameError: global name 'climbStairs' is not defined

    Is it because Leetcode doesn't recognize recursion? Or any other reasons? Thanks.

  • 0

    you have to use self.climbStairs

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.