BFS

N叉树表达
BFS
Prior queue
deque
头取 popleft
尾加 append

DFS
stack

    import collections.deque
    def levelOrder(self, root: 'Node') -> List[List[int]]:
        if not root:
            return []

        ans = list()
        q = deque([root])

        while q:
            cnt = len(q)
            level = list()
            for _ in range(cnt):
                cur = q.popleft()
                level.append(cur.val)
                for child in cur.children:
                    q.append(child)
            ans.append(level)

        return ans

你可能感兴趣的:(BFS)