-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path429.n叉树的层序遍历.py
71 lines (69 loc) · 1.48 KB
/
429.n叉树的层序遍历.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#
# @lc app=leetcode.cn id=429 lang=python
#
# [429] N叉树的层序遍历
#
# https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal/description/
#
# algorithms
# Easy (61.62%)
# Likes: 35
# Dislikes: 0
# Total Accepted: 6.4K
# Total Submissions: 10.4K
# Testcase Example: '{"$id":"1","children":[{"$id":"2","children":[{"$id":"5","children":[],"val":5},{"$id":"6","children":[],"val":6}],"val":3},{"$id":"3","children":[],"val":2},{"$id":"4","children":[],"val":4}],"val":1}'
#
# 给定一个 N 叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。
#
# 例如,给定一个 3叉树 :
#
#
#
#
#
#
#
# 返回其层序遍历:
#
# [
# [1],
# [3,2,4],
# [5,6]
# ]
#
#
#
#
# 说明:
#
#
# 树的深度不会超过 1000。
# 树的节点总数不会超过 5000。
#
#
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution(object):
def levelOrder(self, root):
"""
:type root: Node
:rtype: List[List[int]]
"""
result = []
queue = []
if root is not None:
queue.append(root)
while len(queue) > 0:
new_queue = []
vals = []
for q in queue:
vals.append(q.val)
new_queue.extend(q.children)
result.append(vals)
queue = new_queue
return result