DP/Backtracking: Integer can't keep info in helper function but list can?

I found two very similar questions. One is generate parentheses, another is find the number of paths.
When I return the num_paths from the inner function, the information is lost. Whereas, when I return the list of paths, the list persists.

1.Generate Parenthese
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:

[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]

From Stefan:https://leetcode.com/problems/generate-parentheses/discuss/10096/4-7-lines-Python
def generateParenthesis( n):

def generate(p, left, right, paren=[]):
    if left:
        generate(p + "(", left - 1, right)
    if right > left:
        generate(p + ")", left, right - 1)
    if not right:
        paren.append(p)
    return paren

return generate("", n, n)

2. The idea is that you keep track of the number of East and North steps,which are both n-1.And make sure that East is always great than North. It terminates when both reach 0. Return the result. I think it has the same structure as the problem above,so I wrote the same but replace paren(list) with path_num(integer),however, I get 0 as output because path_num is lost.

0_1522737492524_Screen Shot 2018-04-02 at 11.32.40 PM.png

def num_of_paths_to_dest(n):
if n == 1:
return 1
if n == 0:
return 0
E = N = n-1

def helper(E, N, num_path = 0):
    if E:
      helper(E -1 ,N)
    if  N > E:
      helper(E, N-1)
    if not N:
      num_path += 1
    return num_path

return helper(E,N)

My num_path is lost when it returns, but Stefan's paths (list of parentheses) persists.

Comments (1)