Skip to content

Instantly share code, notes, and snippets.

@EfrainReyes
Created November 9, 2022 02:36
Show Gist options
  • Save EfrainReyes/bcbbf28c4b2f5bd7199d113e469aba9f to your computer and use it in GitHub Desktop.
Save EfrainReyes/bcbbf28c4b2f5bd7199d113e469aba9f to your computer and use it in GitHub Desktop.
339. Nested List Weight Sum
class Solution:
def depthSum(self, nestedList: List[NestedInteger]) -> int:
def depth_sum_recursive(current_item, depth):
if current_item.isInteger():
return current_item.getInteger() * depth
return sum(depth_sum_recursive(item, depth + 1) for item in current_item.getList())
return sum(depth_sum_recursive(item, 1) for item in nestedList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment