Created
November 9, 2022 02:36
-
-
Save EfrainReyes/bcbbf28c4b2f5bd7199d113e469aba9f to your computer and use it in GitHub Desktop.
339. Nested List Weight Sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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