Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values.
For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [3,2,1].
Note: Recursive solution is trivial, could you do it iteratively?
URL: https://leetcode.com/problems/binary-tree-postorder-traversal/
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
if root == None:
return []
else:
stack = []
out_stack = []
stack.append(root)
while stack != []:
current = stack.pop()
out_stack.append(current.val)
if current.left != None:
stack.append(current.left)
if current.right != None:
stack.append(current.right)
return out_stack[::-1]