Leetcode 429. N-ary Tree Level Order Traversal

Posted 周洋的Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 429. N-ary Tree Level Order Traversal相关的知识,希望对你有一定的参考价值。

bfs

class Solution:
    def levelOrder(self, root: ‘Node‘) -> List[List[int]]:
        q,ans=[root],[]
        while q and q[0]:
            ans.append([node.val for node in q])
            q=[child for node in q for child in node.children if child]
        return ans

  

以上是关于Leetcode 429. N-ary Tree Level Order Traversal的主要内容,如果未能解决你的问题,请参考以下文章

(N叉树 BFS) leetcode429. N-ary Tree Level Order Traversal

LeetCode 429. N叉树的层序遍历(N-ary Tree Level Order Traversal)

429. N-ary Tree Level Order Traversal

LeetCode算法题-N-ary Tree Level Order Traversal(Java实现)

429. N-ary Tree Level Order Traversal

429. N-ary Tree Level Order Traversal