LeetCode "Nested List Weight Sum"

An intuitive DFS one.

class Solution(object):
    def go(self, nList, dep):
        ret = 0
        for i in range(len(nList)):
            if(nList[i].isInteger()):
                ret += dep * (nList[i].getInteger())
            else:
                ret += self.go(nList[i].getList(), dep + 1)
        return ret

    def depthSum(self, nestedList):
        return self.go(nestedList, 1)
原文地址:https://www.cnblogs.com/tonix/p/5339629.html