Problem statement

https://leetcode.com/problems/nested-list-weight-sum/

Solution

Here we need to keep a function getSum(elem, depth), which recursively call itself. If elem is Integer (function given to your), then just return elem.getInteger() * depth. In opposite case we need to call getSum(e, depth + 1) for all e in elem.getList(). Actually, we can look at this as graph traversal algorithm, more specifically we use dfs here.

Complexity

Time complexity is $O(n)$, space is $O(h)$, where $h$ is the biggest depth.\

Code

class Solution:    
    def depthSum(self, nestedList):
        return sum([self.getSum(e, 1) for e in nestedList])
        
    def getSum(self, elem, depth):
        if elem.isInteger():
            return elem.getInteger() * depth 
        else:
            return sum([self.getSum(e, depth + 1) for e in elem.getList()])