题目链接: 590. N 叉树的后序遍历
迭代解法。
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
"""
class Solution:
def postorder(self, root: 'Node') -> List[int]:
if root is None:
return []
stack, ret = [root], []
while stack:
node = stack.pop()
ret.append(node.val)
for child in (node.children or []):
stack.append(child)
return ret[::-1]