二叉树:
class Solution:
def postorderTraversal(self, root: TreeNode) -> List[int]:
if not(root):
return []
stack = [root]
res = []
while(stack):
cur_node = stack.pop()
res.append(cur_node.val)
if cur_node.left:
stack.append(cur_node.right)
if cur_node.right:
stack.append(cur_node.left)
return res[::-1]
N叉树:
class Solution:
def postorder(self, root: 'Node') -> List[int]:
if not(root):
return []
stack = [root]
res = []
while(stack):
cur_node = stack.pop()
res.append(cur_node.val)
for child in cur_node.children:
if child:
stack.append(child)
return res[::-1]