Skip to content

Commit 0365a05

Browse files
committed
Time: 45 ms (58.87%), Space: 18.1 MB (77.39%) - LeetHub
1 parent c696dd8 commit 0365a05

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# time complexity: O(m)
2+
# space complexity: O(m)
3+
from typing import List
4+
5+
6+
class Node:
7+
def __init__(self, val=None, children=None):
8+
self.val = val
9+
self.children = children
10+
11+
12+
class Solution:
13+
def postorder(self, root: "Node") -> List[int]:
14+
result = []
15+
if root is None:
16+
return result
17+
nodeStack = [root]
18+
while nodeStack:
19+
currentNode = nodeStack.pop()
20+
result.append(currentNode.val)
21+
for child in currentNode.children:
22+
nodeStack.append(child)
23+
24+
result.reverse()
25+
return result
26+
27+
28+
root = Node(1)
29+
root.children = Node(3)
30+
root.children.children = Node(5)
31+
root.children.children = Node(6)
32+
root.children = Node(2)
33+
root.children = Node(4)

0 commit comments

Comments
 (0)