<?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>construct Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/construct/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/construct/</link>
	<description></description>
	<lastBuildDate>Thu, 03 Oct 2019 16:03:38 +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>construct Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/construct/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-105-construct-binary-tree-from-preorder-and-inorder-traversal/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-105-construct-binary-tree-from-preorder-and-inorder-traversal/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 03 Oct 2019 16:03:28 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[construct]]></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=5705</guid>

					<description><![CDATA[<p>Given preorder and inorder 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-105-construct-binary-tree-from-preorder-and-inorder-traversal/">花花酱 LeetCode 105. Construct Binary Tree from Preorder and Inorder 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 preorder and inorder 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">preorder =&nbsp;[3,9,20,15,7]
inorder = [9,3,15,20,7]</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>Preprocessing: use a hashtable to store the index of element in preorder array.</p>



<p>For an element in inorder array, find the pos of it in preorder array in O(1), anything to the left will be the leftchild and anything to the right will be the right child.</p>



<p>e.g. <br>buildTree([9, 3, 15, 20, 7], [3, 9, 20, 15, 7]):  <br>  root = TreeNode(9) # inorder[0] = 9 <br>  root.left = buildTree([3], [3])<br>  root.right = buildTree([15, 20, 7], [20, 15, 7])<br>  return root</p>



<p></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;preorder, vector&lt;int&gt; &amp;inorder) {
    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[preorder[ps]];
      int pm = ps + (im - is);

      auto root = new TreeNode(preorder[ps]);
      root-&gt;left  = buildTree(is, im - 1, ps + 1, pm);
      root-&gt;right = buildTree(im + 1, ie, pm + 1, pe);
      return root;
    };

    return buildTree(0, inorder.size() - 1, 0, preorder.size() - 1);
  }
};</pre>
</div></div>



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



<ul><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-105-construct-binary-tree-from-preorder-and-inorder-traversal/">花花酱 LeetCode 105. Construct Binary Tree from Preorder and Inorder 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-105-construct-binary-tree-from-preorder-and-inorder-traversal/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1008. Construct Binary Search Tree from Preorder Traversal</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-1008-construct-binary-search-tree-from-preorder-traversal/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-1008-construct-binary-search-tree-from-preorder-traversal/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 10 Mar 2019 06:08:55 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[construct]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4966</guid>

					<description><![CDATA[<p>Return the root node of a binary&#160;search&#160;tree that matches the given&#160;preorder&#160;traversal. (Recall that a binary search tree&#160;is a binary tree where for every&#160;node, any descendant&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-1008-construct-binary-search-tree-from-preorder-traversal/">花花酱 LeetCode 1008. Construct Binary Search Tree from Preorder 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>Return the root node of a binary&nbsp;<strong>search</strong>&nbsp;tree that matches the given&nbsp;<code>preorder</code>&nbsp;traversal.</p>



<p><em>(Recall that a binary search tree&nbsp;is a binary tree where for every&nbsp;node, any descendant of&nbsp;<code>node.left</code>&nbsp;has a value&nbsp;<code>&lt;</code>&nbsp;<code>node.val</code>, and any descendant of&nbsp;<code>node.right</code>&nbsp;has a value&nbsp;<code>&gt;</code>&nbsp;<code>node.val</code>.&nbsp; Also recall that a preorder traversal&nbsp;displays the value of the&nbsp;<code>node</code>&nbsp;first, then traverses&nbsp;<code>node.left</code>, then traverses&nbsp;<code>node.right</code>.)</em></p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[8,5,1,7,10,12]
<strong>Output: </strong>[8,5,10,1,7,null,12]

</pre>



<p><strong>Note:</strong>&nbsp;</p>



<ol><li><code>1 &lt;= preorder.length &lt;= 100</code></li><li>The values of&nbsp;<code>preorder</code>&nbsp;are distinct.</li></ol>



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



<p>Time complexity: O(n^2)<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, 8 ms, 10.8 MB
class Solution {
public:
  TreeNode* bstFromPreorder(vector&lt;int&gt;&amp; preorder) {
    return build(preorder, 0, preorder.size());
  }
private:
  TreeNode* build(const vector&lt;int&gt;&amp; preorder, int s, int e) {
    if (s &gt;= e) return nullptr;
    auto root = new TreeNode(preorder[s]);
    int m = s;
    while (m &lt; e &amp;&amp; preorder[m] &lt;= root-&gt;val) ++m;
    root-&gt;left = build(preorder, s + 1, m);
    root-&gt;right = build(preorder, m, e);
    return root;
  }
};</pre>

</div><h2 class="tabtitle">C++/iterator</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution {
public:
  TreeNode* bstFromPreorder(vector&lt;int&gt;&amp; preorder) {
    return build(begin(preorder), end(preorder));
  }
private:
  typedef vector&lt;int&gt;::const_iterator IT;
  TreeNode* build(IT s, IT e) {
    if (s &gt;= e) return nullptr;
    auto root = new TreeNode(*s);
    IT m = s;
    while (m &lt; e &amp;&amp; *m &lt;= *s) ++m;
    root-&gt;left = build(s + 1, m);
    root-&gt;right = build(m, e);
    return root;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-1008-construct-binary-search-tree-from-preorder-traversal/">花花酱 LeetCode 1008. Construct Binary Search Tree from Preorder 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-1008-construct-binary-search-tree-from-preorder-traversal/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 984. String Without AAA or BBB</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-984-string-without-aaa-or-bbb/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-984-string-without-aaa-or-bbb/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 27 Jan 2019 18:42:07 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[construct]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4709</guid>

					<description><![CDATA[<p>Given two integers&#160;A&#160;and&#160;B, return&#160;any&#160;string&#160;S&#160;such that: S&#160;has length&#160;A + B&#160;and contains exactly&#160;A&#160;'a'&#160;letters, and exactly&#160;B&#160;'b'&#160;letters; The substring&#160;'aaa'&#160;does not occur in&#160;S; The substring&#160;'bbb'&#160;does not occur in&#160;S. Example 1:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-984-string-without-aaa-or-bbb/">花花酱 LeetCode 984. String Without AAA or BBB</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 two integers&nbsp;<code>A</code>&nbsp;and&nbsp;<code>B</code>, return&nbsp;<strong>any</strong>&nbsp;string&nbsp;<code>S</code>&nbsp;such that:</p>



<ul><li><code>S</code>&nbsp;has length&nbsp;<code>A + B</code>&nbsp;and contains exactly&nbsp;<code>A</code>&nbsp;<code>'a'</code>&nbsp;letters, and exactly&nbsp;<code>B</code>&nbsp;<code>'b'</code>&nbsp;letters;</li><li>The substring&nbsp;<code>'aaa'</code>&nbsp;does not occur in&nbsp;<code>S</code>;</li><li>The substring&nbsp;<code>'bbb'</code>&nbsp;does not occur in&nbsp;<code>S</code>.</li></ul>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>A = 1, B = 2
<strong>Output: </strong>"abb"
<strong>Explanation:</strong> "abb", "bab" and "bba" are all correct answers.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>A = 4, B = 1
<strong>Output: </strong>"aabaa"</pre>



<p><strong>Note:</strong></p>



<ol><li><code>0 &lt;= A &lt;= 100</code></li><li><code>0 &lt;= B &lt;= 100</code></li><li>It is guaranteed such an&nbsp;<code>S</code>&nbsp;exists for the given&nbsp;<code>A</code>&nbsp;and&nbsp;<code>B</code>.</li></ol>



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

<pre class="crayon-plain-tag">class Solution {
public:
  string strWithout3a3b(int A, int B) {
    char a = 'a';
    char b = 'b';
    if (B &gt; A) {
      swap(A, B);
      swap(a, b);
    }
    string ans;
    while (A || B) {
      if (A &gt; 0) { ans += a; --A; }
      if (A &gt; B) { ans += a; --A; }
      if (B &gt; 0) { ans += b; --B; }
    }
    return ans;
  }
};</pre>

</div><h2 class="tabtitle">C++ / overload</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution {
public:
  string strWithout3a3b(int A, int B, char a = 'a', char b = 'b') {
    if (B &gt; A) return strWithout3a3b(B, A, b, a);
    string ans;
    while (A || B) {
      if (A &gt; 0) { ans += a; --A; }
      if (A &gt; B) { ans += a; --A; }
      if (B &gt; 0) { ans += b; --B; }
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-984-string-without-aaa-or-bbb/">花花酱 LeetCode 984. String Without AAA or BBB</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/greedy/leetcode-984-string-without-aaa-or-bbb/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
