Solved by Python iteratively


  • 0
    C
    class Solution:
        def postorderTraversal(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            res = []
            stk = []
            stk.append(root)
            if not root:
                return res
            while stk:
                node = stk.pop()
                res.append(node.val)
                if node.left:
                    stk.append(node.left)
                if node.right:
                    stk.append(node.right)
            return res[::-1]
    

    The solution can be improved, because I suppose the final reversion is unnecessary.


Log in to reply
 

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