Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 16554bd

Browse files
Time: 33 ms (99.22%), Space: 18.1 MB (77.39%) - LeetHub
1 parent feb48d1 commit 16554bd

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
# Definition for a Node.
3+
class Node:
4+
def __init__(self, val=None, children=None):
5+
self.val = val
6+
self.children = children
7+
"""
8+
9+
class Solution:
10+
def postorder(self, root: 'Node') -> List[int]:
11+
if not root:
12+
return []
13+
14+
stack = [root]
15+
result = []
16+
17+
while stack:
18+
node = stack.pop()
19+
result.append(node.val)
20+
# Add all children to stack
21+
for child in node.children:
22+
stack.append(child)
23+
24+
# Since we're effectively collecting the nodes in reverse order, reverse the result
25+
return result[::-1]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /