{"id":22513955,"url":"https://github.com/emahtab/binary-tree-postorder-traversal","last_synced_at":"2026-01-07T01:42:48.364Z","repository":{"id":79525311,"uuid":"233342564","full_name":"eMahtab/binary-tree-postorder-traversal","owner":"eMahtab","description":null,"archived":false,"fork":false,"pushed_at":"2020-04-21T05:50:07.000Z","size":47,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-02T03:26:12.295Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-01-12T05:20:54.000Z","updated_at":"2020-04-21T05:50:09.000Z","dependencies_parsed_at":"2023-05-10T17:00:27.049Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/binary-tree-postorder-traversal","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-postorder-traversal","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-postorder-traversal/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-postorder-traversal/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-postorder-traversal/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/binary-tree-postorder-traversal/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245952293,"owners_count":20699464,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-07T03:15:16.710Z","updated_at":"2026-01-07T01:42:48.338Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Binary Tree Postorder Traversal 🌲\n## https://leetcode.com/problems/binary-tree-postorder-traversal\n\nGiven a binary tree, return the postorder traversal of its nodes' values.\n```\nExample:\n\nInput: [1,null,2,3]\n   1\n    \\\n     2\n    /\n   3\n\nOutput: [3,2,1]\n```\n**Follow up: Recursive solution is trivial, could you do it iteratively?**\n\n# Postorder Traversal :\n![Binary Tree](binary-tree.PNG?raw=true \"Binary Tree\")\n\nNote that node 75 doesn't have left child and node 29 doesn't have right child.\n\nThe postorder traversal for above binary tree will be **[21, 20, 35, 30, 75, 67, 50, 29, 60, 65, 24, 43, 70]**\n\n## Implementation : Recursive\n\n```java\nimport java.util.ArrayList;\nimport java.util.List;\n\npublic class App {\n\n     public static void main(String[] args) {\n\t   TreeNode root = new TreeNode(70);\n           root.left = new TreeNode(67); root.right = new TreeNode(43);\n\t\t\n\t   root.left.left = new TreeNode(35); root.left.right = new TreeNode(75); \n\t\t\n           root.left.left.left = new TreeNode(21); root.left.left.right = new TreeNode(20);\n\t\t\n           root.left.right.right = new TreeNode(30);\n\t\t\n           root.right.left = new TreeNode(29); root.right.left.left = new TreeNode(50);\n\t\t\n           root.right.right = new TreeNode(24); \n\t\t\n\t   root.right.right.left = new TreeNode(60); root.right.right.right = new TreeNode(65);\n           System.out.println(inorderTraversal(root));\n     }\n\t\n\t\n     // Definition for a binary tree node.\n     static public class TreeNode {\n\t      int val;\n\t      TreeNode left;\n\t      TreeNode right;\n\t      TreeNode(int x) { val = x; }\n     }\n\t \n\t\n     public static List \u003cInteger\u003e inorderTraversal(TreeNode root) {\n        List \u003cInteger\u003e res = new ArrayList\u003cInteger\u003e();\n        helper(root, res);\n        return res;\n     }\n\n     public static void helper(TreeNode root, List \u003cInteger\u003e res) {\n        if (root != null) {\n            helper(root.left, res);\n            helper(root.right, res);\n            res.add(root.val);\n        }\n     }\n}\n\n```\n## Implementation : Iterative (One Stack)\n\n```java\npublic List\u003cInteger\u003e postorderTraversal(TreeNode root) {\n      List\u003cInteger\u003e res = new ArrayList\u003c\u003e();\n      if(root == null)\n          return res;\n      Stack\u003cTreeNode\u003e stack = new Stack\u003cTreeNode\u003e();\n      stack.push(root);\n      while(!stack.isEmpty()){\n          TreeNode current = stack.pop();\n          res.add(0,current.val);\n          if(current.left != null){\n              stack.push(current.left);\n          }\n          if(current.right != null){\n              stack.push(current.right);\n          }\n      }  \n   return res;  \n}\n```\n#################################### OR ##############################\n```java\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode(int x) { val = x; }\n * }\n */\nclass Solution {\n    public List\u003cInteger\u003e postorderTraversal(TreeNode root) {\n        List\u003cInteger\u003e postorder = new ArrayList\u003c\u003e();\n        if(root == null)\n            return postorder;\n        Stack\u003cTreeNode\u003e stack = new Stack\u003c\u003e();\n        stack.push(root);\n        while(!stack.isEmpty()) {\n            TreeNode current = stack.pop();\n            postorder.add(current.val);\n            if(current.left != null)\n                stack.push(current.left);\n            if(current.right != null)\n                stack.push(current.right);\n        }\n        Collections.reverse(postorder);\n        return postorder;\n    }\n}\n```\n\n## Implementation : Iterative (Two Stack)\n\n```java\npublic List\u003cInteger\u003e postorderTraversal(TreeNode root) {\n      List\u003cInteger\u003e res = new ArrayList\u003c\u003e();\n      if(root == null)\n          return res;\n      Stack\u003cTreeNode\u003e stack = new Stack\u003cTreeNode\u003e();\n      Stack\u003cInteger\u003e values = new Stack\u003c\u003e();  \n      stack.push(root);\n      while(!stack.isEmpty()){\n          TreeNode current = stack.pop();\n          values.push(current.val); \n          if(current.left != null){\n              stack.push(current.left);\n          }\n          if(current.right != null){\n              stack.push(current.right);\n          }\n      } \n      while(!values.isEmpty()){\n          res.add(values.pop());\n      }\n      return res;  \n}\n```\n\n# References :\n1. https://www.youtube.com/watch?v=sMI4RBEZyZ4\n2. https://www.techiedelight.com/postorder-tree-traversal-iterative-recursive\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fbinary-tree-postorder-traversal","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fbinary-tree-postorder-traversal","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fbinary-tree-postorder-traversal/lists"}