我看到了a problem on leetcode的以下解决方案:
class Solution:
def levelOrder(self, root):
"""
:type root: Node
:rtype: List[List[int]]
"""
if root is None:
return []
q, res = [root], []
q1 = []
while q:
res.append([node.val for node in q])
q = [child for node in q for child in node.children]
return res
[child for node in q for child in node.children]
如何工作?将child放在for循环语句之前意味着什么?
答案 0 :(得分:8)
这是一个嵌套列表理解。
volume:
等效于:
q = [child for node in q for child in node.children]