Linkedin leetcode 339

image

Reference:

def DFS(nestedList, depth):
            temp_sum = 0
            for member in nestedList:
                if member.isInteger():
                    temp_sum += member.getInteger() * depth
                else:
                    temp_sum += DFS(member.getList(),depth+1)
            return temp_sum
        return DFS(nestedList,1)

当在DFS里面CALL DFS 的时候, 第一步不是把temp_sum 置0 了吗, 为什么最后还能是累加的结果?

The recursion function returns the sum from bottom up.
It is like:

1 -> 2 -> 3 -> 4 -> 5

and then for each recursion:
sum1 <- sum2 <- sum3 <- sum4 <- sum5

1 Like