-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsolution.py
48 lines (44 loc) · 1.1 KB
/
solution.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
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
"""
"""
38 / 38 test cases passed.
Runtime: 56 ms
Memory Usage: 16.9 MB
"""
class Solution:
def __init__(self):
self.ans = []
def postorder(self, root: 'Node') -> List[int]:
if root:
for child in root.children:
self.postorder(child)
self.ans.append(root.val)
return self.ans
"""
38 / 38 test cases passed.
Runtime: 48 ms
Memory Usage: 16.9 MB
"""
class Solution2:
def postorder(self, root: 'Node') -> List[int]:
ans = []
if not root:
return ans
stk = []
while root or stk:
while root:
stk.append(root)
children = root.children
root = children.pop(0) if children else None
root = stk[-1]
if not root.children:
ans.append(stk.pop().val)
root = None
else:
root = root.children.pop(0)
return ans