Python using two stacks(lists)


  • 0
    C

    class Solution(object):

    def postorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root:
            return []
        st1=[]
        st2=[]
        res=[]
        st1.append(root)
        while st1:
            cur = st1.pop(-1)
            st2.append(cur)
            if cur.left:
                st1.append(cur.left)
            if cur.right:
                st1.append(cur.right)
            
        while st2:
            cur = st2.pop(-1)
            res.append(cur.val)
        
        return res

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.