python3实现leecode广度遍历
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
if root is None:
return []
cur = [root]
next = []
result = []
sub = []
while cur:
node = cur.pop(0)
sub.append(node.val)
if node.left is not None:
next.append(node.left)
if node.right is not None:
next.append(node.right)
if not cur and next:
result.append(sub)
sub = []
cur = next
next = []
if sub:
result.append(sub)
return result