{"id":5708,"date":"2019-10-03T09:18:18","date_gmt":"2019-10-03T16:18:18","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5708"},"modified":"2019-10-03T09:19:09","modified_gmt":"2019-10-03T16:19:09","slug":"leetcode-106-construct-binary-tree-from-inorder-and-postorder-traversal","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-106-construct-binary-tree-from-inorder-and-postorder-traversal\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal"},"content":{"rendered":"\n<p>Given inorder and postorder traversal of a tree, construct the binary tree.<\/p>\n\n\n\n<p><strong>Note:<\/strong><br>You may assume that duplicates do not exist in the tree.<\/p>\n\n\n\n<p>For example, given<\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">inorder =&nbsp;[9,3,15,20,7]\npostorder = [9,15,7,20,3]<\/pre>\n\n\n\n<p>Return the following binary tree:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">    3\n   \/ \\\n  9  20\n    \/  \\\n   15   7<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion<\/strong><\/h2>\n\n\n\n<p>Similar to <a href=\"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-105-construct-binary-tree-from-preorder-and-inorder-traversal\/\">LC 105<\/a><\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {\n    unordered_map<int, int> pos;\n \n    for (int i = 0; i < inorder.size(); i++)\n      pos[inorder[i]] = i;\n \n    function<TreeNode*(int, int, int, int)> buildTree = [&](int is, int ie, int ps, int pe) {\n      if (ps > pe) return (TreeNode*)nullptr;\n \n      int im = pos[postorder[pe]];\n      int pm = ps + (im - is) - 1;\n \n      auto root = new TreeNode(postorder[pe]);\n      root->left = buildTree(is, im - 1, ps, pm);\n      root->right = buildTree(im + 1, ie, pm + 1, pe - 1);\n      return root;\n    };\n \n    return buildTree(0, inorder.size() - 1, 0, postorder.size() - 1);\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Related Problems<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-105-construct-binary-tree-from-preorder-and-inorder-traversal\/\">https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-105-construct-binary-tree-from-preorder-and-inorder-traversal\/<\/a><\/li><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-1008-construct-binary-search-tree-from-preorder-traversal\/\">https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-1008-construct-binary-search-tree-from-preorder-traversal\/<\/a><\/li><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-889-construct-binary-tree-from-preorder-and-postorder-traversal\/\">https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-889-construct-binary-tree-from-preorder-and-postorder-traversal\/<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. For example,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[507,82,376,17,28],"class_list":["post-5708","post","type-post","status-publish","format-standard","hentry","category-tree","tag-cosntruct","tag-hashtable","tag-on","tag-recursion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5708"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5708\/revisions"}],"predecessor-version":[{"id":5710,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5708\/revisions\/5710"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}