Last active
April 2, 2020 07:49
-
-
Save nhudinhtuan/44d4bf1680e55076659c13563f9283cb to your computer and use it in GitHub Desktop.
Tree traversal - postorder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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