Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created December 8, 2018 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/4fb23b547a99847aa494fe1918039b42 to your computer and use it in GitHub Desktop.
Save jianminchen/4fb23b547a99847aa494fe1918039b42 to your computer and use it in GitHub Desktop.
Post order iterative solution - find first element in post order traversal
class Solution:
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
stack = [root]
res = []
while stack:
s = stack[-1]
if s.left:
stack.append(s.left)
elif s.right:
stack.append(s.right)
else:
res.append(stack.pop())
# stack1 = [root]
# stack2 = []
# while stack1:
# node = stack1.pop()
# if node:
# stack2.append(node.left)
# stack2.append(node.right)
# return stack2[::-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment