{"id":22513945,"url":"https://github.com/emahtab/construct-binary-tree-from-preorder-and-postorder-traversal","last_synced_at":"2026-01-07T13:03:59.053Z","repository":{"id":79525382,"uuid":"233414207","full_name":"eMahtab/construct-binary-tree-from-preorder-and-postorder-traversal","owner":"eMahtab","description":"Construct Binary Tree from Preorder and Postorder Traversal","archived":false,"fork":false,"pushed_at":"2020-01-19T14:25:53.000Z","size":27,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-02T03:26:11.517Z","etag":null,"topics":["binary-tree","binary-tree-construction","leetcode","problem-solving"],"latest_commit_sha":null,"homepage":"","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-12T15:30:38.000Z","updated_at":"2020-01-19T14:27:31.000Z","dependencies_parsed_at":"2023-05-10T17:15:51.415Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/construct-binary-tree-from-preorder-and-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%2Fconstruct-binary-tree-from-preorder-and-postorder-traversal","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fconstruct-binary-tree-from-preorder-and-postorder-traversal/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fconstruct-binary-tree-from-preorder-and-postorder-traversal/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fconstruct-binary-tree-from-preorder-and-postorder-traversal/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/construct-binary-tree-from-preorder-and-postorder-traversal/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245952235,"owners_count":20699452,"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":["binary-tree","binary-tree-construction","leetcode","problem-solving"],"created_at":"2024-12-07T03:15:13.401Z","updated_at":"2026-01-07T13:03:54.033Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"## Construct Binary Tree from Preorder and Postorder Traversal\n### https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal\n\nReturn any binary tree that matches the given preorder and postorder traversals.\n\nValues in the traversals pre and post are distinct positive integers.\n```\nExample 1:\n\nInput: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1]\nOutput: [1,2,3,4,5,6,7]\n``` \n\n**Note:**\n1. 1 \u003c= pre.length == post.length \u003c= 30\n2. pre[] and post[] are both permutations of 1, 2, ..., pre.length.\n3. It is guaranteed an answer exists. If there exists multiple answers, you can return any of them.\n\n## Thoughts \n\nWe can construct a unique binary tree from inorder and preorder sequences, as well as from the inorder and postorder sequences. \nBut preorder and postorder sequences don’t provide enough information to create a unique binary tree. \nMore than one binary trees can be constructed due to ambiguity.\n\n```\n              a             a         \n             /               \\\n            b                 b \n           /                   \\\n          c                     c\n Left Skewed Tree            Right Skewed Tree \n```\nFor both left skewed tree and right skewed tree, the preorder and postorder traversal results in same sequence.\n\n```\nPreorder  [a, b, c]\nPostorder [c, b, a]\n```\nTherefore, its not possible to construct a unique binary tree with the help of preorder and postoder sequences. \nHowever we can construct a unique full binary tree using the Preorder and Postorder traversal.\n\n## Approach :\n\n![Full Binary Tree](Full-Binary-Tree.png?raw=true \"Full Binary Tree\")\n\nPreorder  :  { 1, `2, 4, 5`, `3, 6, 8, 9, 7` }\n\nPostorder :  { `4, 5, 2`, `8, 9, 6, 7, 3`, 1 }\n\nWe know that root is the first element in preorder sequence and the last element in postorder sequence. Therefore, the root node is 1. Then we locate the next element in preorder sequence, which must be the left child of the root node. In this case, the left child is 2. \n\nNow since 2 is root node of the left subtree, all nodes before 2 in the postorder sequence must be present in the left subtree of the root node i.e.  {4, 5, 2}  and all the nodes after 2 (except the last) must be present in right subtree i.e.  {8, 9, 6, 7, 3}\n\n**Left Subtree**\n```\nPreorder  [2, 4, 5]\nPostorder [4, 5, 2]\n```\n**Right Subtree**\n```\nPreorder  [3, 6, 8, 9, 7]\nPostorder [8, 9, 6, 7, 3]\n```\n\n## Implementation :\n\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 static TreeNode constructFromPrePost(int[] pre, int[] post) {\n\tint preStart = 0;\n\tint preEnd = pre.length - 1;\n\tint postStart = 0;\n\tint postEnd = post.length - 1;\n\t      \n\tMap\u003cInteger, Integer\u003e map = new HashMap\u003c\u003e();\n\tfor(int i = 0; i \u003c post.length; i++) {\n\t    map.put(post[i], i);\n\t}\n\t      \n       return construct(pre, preStart, preEnd, post, postStart, postEnd, map);\n     }\n\t\n     private static TreeNode construct(int[] pre, int preStart, int preEnd, \n\t\t\t               int[] post, int postStart, int postEnd, Map\u003cInteger, Integer\u003e map) {\n\t if(preStart \u003e preEnd || postStart \u003e postEnd)\n\t      return null;\n\t\t\n\t TreeNode node = new TreeNode(pre[preStart]);\n\t if(preStart+1 \u003c= preEnd) {\n\t      int rootIndex = map.get(pre[preStart + 1]);\n\t      int leftSubtreeSize = rootIndex - postStart;\n\t      node.left = construct(pre, preStart + 1, preStart+1+leftSubtreeSize, \n\t\t\t\t       post, postStart, postStart+leftSubtreeSize, map);\n\t      node.right = construct(pre, preStart+1+leftSubtreeSize+1, preEnd, \n\t\t\t\t       post, rootIndex+1, postEnd - 1, map);\n\t  }\n\t\t\n\treturn node;\n     }\n}\n```\n\n## References :\nhttps://www.techiedelight.com/construct-full-binary-tree-from-preorder-postorder-sequence\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fconstruct-binary-tree-from-preorder-and-postorder-traversal","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fconstruct-binary-tree-from-preorder-and-postorder-traversal","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fconstruct-binary-tree-from-preorder-and-postorder-traversal/lists"}