Post Order Traversal of a Binary Tree (Leetcode 145)

Left -> Right -> Node

Problem Link: https://leetcode.com/problems/binary-tree-postorder-traversal/

class Solution:
    def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
         
        self.s=[]
        self.ans=[]
        curr=root
        while(1):
            if(curr is None and  len(self.s)==0):
                return self.ans
            if(curr is not None):
                self.ans.insert(0,curr.val)
                self.s.append(curr)
                curr=curr.right
            else:
                curr=self.s.pop()
                curr=curr.left
        return self.ans

Last updated