-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
34 lines (29 loc) · 802 Bytes
/
solution.py
File metadata and controls
34 lines (29 loc) · 802 Bytes
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
"""
# 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]]
"""
res = []
stack = []
next_stack = []
values = []
if root: stack.append(root)
while len(stack) > 0:
node = stack[0]
stack = stack[1:]
values.append(node.val)
next_stack += node.children
if len(stack) == 0:
res.append(values)
values = []
stack = next_stack
next_stack = []
return res