-
Notifications
You must be signed in to change notification settings - Fork 1
/
question_3.py
62 lines (50 loc) · 1.05 KB
/
question_3.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/8/2 10:18
# @Author : cancan
# @File : question_3.py
# @Function : N叉树的层序遍历
"""
Question:
给定一个N叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。
例如,给定一个 3叉树 :
1
/ | \
3 2 4
/ \
5 6
返回其层序遍历:
[
[1],
[3,2,4],
[5,6]
]
Note:
1.树的深度不会超过 1000。
2.树的节点总数不会超过 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]]
"""
if not root:
return []
r = []
t = [root]
while t:
r.append([i.val for i in t])
temp = []
for i in t:
for j in i.children:
temp.append(j)
t = temp
return r