LeetCode #145 Binary Tree Postorder Traversal Solution & Explanation

LeetCode Problem

Given the root of a binary tree, return the postorder traversal of its nodes’ values.

LeetCode#145 Examples
LeetCode#145 Constraints

Follow up: Recursive solution is trivial, could you do it iteratively?


Solution

There have three common different ways the binary tree can be traversed .

Which are Inorder, Preorder and Postorder.

Postorder traversal : Left Node ➡ Right Node ➡ Parent Node ( Root )

C# Solution

Solution1

public class Solution {
    public IList<int> PostorderTraversal(TreeNode root) {
        List<int>res = new List<int>();
        if(root == null)
        {
            return res;
        } 
        return Postorder(res,root);
    }
    
    public IList<int> Postorder(IList<int> res,TreeNode root)
    {   
        if(root.left!=null)
        {
           res = Postorder(res,root.left); 
        }
        
        if(root.right!=null)
        {
            res = Postorder(res,root.right); 
        }
        
        res.Add(root.val);
        
        
        return res;
    }
}

Java Solution

Solution1

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer>res = new ArrayList<Integer>();
        if(root == null)
        {
            return res;
        } 
        return Postorder(res,root);
    }
    
    public List<Integer> Postorder(List<Integer> res,TreeNode root)
    {     
        if(root.left!=null)
        {
           res = Postorder(res,root.left); 
        }
          
        if(root.right!=null)
        {
            res = Postorder(res,root.right); 
        }
       
        res.add(root.val);
        
        return res;
    }
}

Python3 Solution

Solution1

class Solution:
    def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
              
        def Postorder(root): 
            if(root.left != None):
                Postorder(root.left)

            if(root.right != None):
                Postorder(root.right)
                
            res.append(root.val)
            
        
        res = []
        if(root == None):
            return res
        
        Postorder(root)
        return res 

JavaScript Solution

Solution1

var postorderTraversal = function(root) {
    res = [];
    if(root == undefined)
    {
        return res;
    }
    return Postorder(res,root);
};

function Postorder(res,root)
{
    if(root.left!=undefined)
    {
       res = Postorder(res,root.left); 
    }

    if(root.right!=undefined)
    {
        res = Postorder(res,root.right); 
    }
    
    res.push(root.val);
    
    return res; 
}

Conclusion

🧡If my solution helps, that is my honor!

If you got any problem about the explanation, please feel free to let me know

The problem link : Binary Tree Postorder Traversal – LeetCode

Similar problems : Zyrastory.com – LeetCode #94 Binary Tree Inorder Traversal

Zyrastory.com – LeetCode #144 Binary Tree Preorder Traversal Solution & Explanation

Latest Post

Leave a Reply

Your email address will not be published. Required fields are marked *