Skip to content

Instantly share code, notes, and snippets.

@nhudinhtuan
Last active April 2, 2020 07:49
Show Gist options
  • Save nhudinhtuan/44d4bf1680e55076659c13563f9283cb to your computer and use it in GitHub Desktop.
Save nhudinhtuan/44d4bf1680e55076659c13563f9283cb to your computer and use it in GitHub Desktop.
Tree traversal - postorder
def postorder_traversal_iterating(root):
if not root:
return []
result = []
stack = [root]
# get the result in order of node, right, left
while stack:
current = stack.pop()
if current.left:
stack.append(current.left)
if current.right:
stack.append(current.right)
# visit node
result.append(current.val)
# reverse the result to get the target output as left, right node
return result[::-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment