Created
December 8, 2018 21:03
-
-
Save jianminchen/4fb23b547a99847aa494fe1918039b42 to your computer and use it in GitHub Desktop.
Post order iterative solution - find first element in post order traversal
This file contains 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
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