<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>cosntruct Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/cosntruct/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/cosntruct/</link>
	<description></description>
	<lastBuildDate>Thu, 03 Oct 2019 16:19:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>cosntruct Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/cosntruct/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-106-construct-binary-tree-from-inorder-and-postorder-traversal/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-106-construct-binary-tree-from-inorder-and-postorder-traversal/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 03 Oct 2019 16:18:18 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[cosntruct]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[O(n)]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5708</guid>

					<description><![CDATA[<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>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-106-construct-binary-tree-from-inorder-and-postorder-traversal/">花花酱 LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given inorder and postorder traversal of a tree, construct the binary tree.</p>



<p><strong>Note:</strong><br>You may assume that duplicates do not exist in the tree.</p>



<p>For example, given</p>



<pre class="wp-block-preformatted;crayon:false">inorder =&nbsp;[9,3,15,20,7]
postorder = [9,15,7,20,3]</pre>



<p>Return the following binary tree:</p>



<pre class="wp-block-preformatted;crayon:false">    3
   / \
  9  20
    /  \
   15   7</pre>



<h2><strong>Solution: Recursion</strong></h2>



<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>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  TreeNode *buildTree(vector&lt;int&gt; &amp;inorder, vector&lt;int&gt; &amp;postorder) {
    unordered_map&lt;int, int&gt; pos;
 
    for (int i = 0; i &lt; inorder.size(); i++)
      pos[inorder[i]] = i;
 
    function&lt;TreeNode*(int, int, int, int)&gt; buildTree = [&amp;](int is, int ie, int ps, int pe) {
      if (ps &gt; pe) return (TreeNode*)nullptr;
 
      int im = pos[postorder[pe]];
      int pm = ps + (im - is) - 1;
 
      auto root = new TreeNode(postorder[pe]);
      root-&gt;left = buildTree(is, im - 1, ps, pm);
      root-&gt;right = buildTree(im + 1, ie, pm + 1, pe - 1);
      return root;
    };
 
    return buildTree(0, inorder.size() - 1, 0, postorder.size() - 1);
  }
};</pre>
</div></div>



<h2><strong>Related Problems</strong></h2>



<ul><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>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-106-construct-binary-tree-from-inorder-and-postorder-traversal/">花花酱 LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/tree/leetcode-106-construct-binary-tree-from-inorder-and-postorder-traversal/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
