-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path042_preorder.py
More file actions
35 lines (32 loc) · 1.01 KB
/
042_preorder.py
File metadata and controls
35 lines (32 loc) · 1.01 KB
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
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
"""
class Solution:
def __init__(self):
self.list1 = []
self.flag = True
def preorder(self, root: 'Node') -> List[int]:
if root == None:
return []
elif root.children == None:
return [root.val]
elif root.children != None:
for node in root.children:
if self.flag:
self.list1.append(root.val)
self.flag = False
self.list1.append(node.val)
self.preorder(node)
return self.list1
# -------------------------------------------------------
if not root:
return []
result = [root.val]
if root.children is not None:
for child in root.children:
result.extend(self.preorder(child))
return result