LeetCode 429. N-ary Tree Level Order Traversal

简介: 给定一个 N 叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。

v2-5eb0d43e2d730e0830f3b5e5bc8a31f5_1440w.jpg

Description



Given an n-ary tree, return the level order traversal of its nodes' values.

Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).


Example 1:

image.png



Input: root = [1,null,3,2,4,null,5,6]
Output: [[1],[3,2,4],[5,6]]
Example 2:



v2-b669aeec37e0c229b75fbf08bccaa8f1_720w.jpg


Example 2:


Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: [[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]


Constraints:

The height of the n-ary tree is less than or equal to 1000

The total number of nodes is between [0, 10^4]


描述



给定一个 N 叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。


例如,给定一个 3叉树 :

返回其层序遍历:


[
     [1],
     [3,2,4],
     [5,6]
]


说明:

树的深度不会超过 1000。

树的节点总数不会超过 5000。


来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


思路



  • 层次遍历,使用队列。
  • 将当前层的所有节点押入队中,记下当前队列中元素的个数。
  • for 循环取出当前层的元素,每取一个元素,将其的 children 元素押入队列尾部。


# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-12-28 21:23:06
# @Last Modified by:   何睿
# @Last Modified time: 2019-12-28 21:34:42
from typing import List
from collections import deque
class Solution:
    def levelOrder(self, root: 'Node') -> List[List[int]]:
        if not root:
            return []
        result = []
        queue = deque([root])
        while queue:
            tmp = []
            count = len(queue)
            for _ in range(count):
                node = queue.popleft()
                tmp.append(node.val)
                if node.children:
                    queue.extend(node.children)
            result.append(tmp)
        return result

源代码文件在 这里


目录
相关文章
|
8月前
|
Java
Leetcode 114. Flatten Binary Tree to Linked List
思路也很简单,先把root的左子树(如有)变成单链表 leftlinkedlist,把root的右子树(如有)变成单链表 rightlinkedlist,再把root的右节点变成leftlikedlist,再把rightlinkedlist接到leftlinkedlist后面,代码如下。
25 1
|
8月前
Leetcode Minimum Depth of Binary Tree (面试题推荐)
计算树的最小深度 很简单的一道题,只需要遍历一次树,到叶子节点的时候计算一下深度和当前最小深度比较,保存最小值就行。 我在这用了一个全局变量 mindepth。总感觉我这代码写的不够简练,求更精简的方法。
32 0
|
8月前
Leetcode Binary Tree Postorder Traversal(面试题推荐)
非递后续归遍历二叉树,肯定得用到栈。先序遍历很好写,但后续遍历就不是那么容易了。 只需要设置个指针pre,指向最后输出的那个节点就行了,只要判断cur指针指向的是上次输出节点的父节点,且cur无其他未遍历的节点,这个时候就把cur节点输出即可,然后更改pre。原理是要遍历当前节点,其所有子节点都必须遍历完,因为肯定是先左后右,所以只需一个指针保持前一次输出的结果即可。
31 0
|
8月前
Leetcode 236. Lowest Common Ancestor of a Binary Tree
根据LCA的定义,二叉树中最小公共祖先就是两个节点p和q最近的共同祖先节点,LCA的定义没什么好解释的,主要是这道题的解法。
22 0
|
8月前
Leetcode 623. Add One Row to Tree
题目很简单,在树的第d层加一层,值为v。递归增加一层就好了。代码如下
30 0
LeetCode contest 190 5418. 二叉树中的伪回文路径 Pseudo-Palindromic Paths in a Binary Tree
LeetCode contest 190 5418. 二叉树中的伪回文路径 Pseudo-Palindromic Paths in a Binary Tree
|
算法 Python
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 102. 二叉树的层序遍历 Binary Tree Level Order Traversal
LeetCode 102. 二叉树的层序遍历 Binary Tree Level Order Traversal
LeetCode 104. 二叉树的最大深度 Maximum Depth of Binary Tree
LeetCode 104. 二叉树的最大深度 Maximum Depth of Binary Tree
|
1月前
|
算法 C++
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题-2
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题