class Solution(object):
def depthSum(self, nestedList):
"""
:type nestedList: List[NestedInteger]
:rtype: int
"""
depth, ret = 1, 0
while nestedList:
ret += depth * sum([x.getInteger() for x in nestedList if x.isInteger()])
nestedList = sum([x.getList() for x in nestedList if not x.isInteger()], [])
depth += 1
return ret
Short Python BFS


Very succinct solution!
Could you explain why the sum function in the following line works?
sum([x.getList() for x in nestedList if not x.isInteger()], [])
It is not clear to me from the documentation in the python source code (see below).
Signature: sum(iterable, start=0, /)
Docstring:
Return the sum of a 'start' value (default: 0) plus an iterable of numbers
When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values and may
reject nonnumeric types.
Type: builtin_function_or_method
