<?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>insert Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/insert/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/insert/</link>
	<description></description>
	<lastBuildDate>Sun, 22 Mar 2020 20:53:07 +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>insert Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/insert/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1389. Create Target Array in the Given Order</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-1389-create-target-array-in-the-given-order/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-1389-create-target-array-in-the-given-order/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 22 Mar 2020 20:52:58 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[insert]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6535</guid>

					<description><![CDATA[<p>Given two arrays of integers&#160;nums&#160;and&#160;index. Your task is to create&#160;target&#160;array under the following rules: Initially&#160;target&#160;array is empty. From left to right read nums[i] and index[i],&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1389-create-target-array-in-the-given-order/">花花酱 LeetCode 1389. Create Target Array in the Given Order</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 arrays of integers&nbsp;<code>nums</code>&nbsp;and&nbsp;<code>index</code>. Your task is to create&nbsp;<em>target</em>&nbsp;array under the following rules:</p>



<ul><li>Initially&nbsp;<em>target</em>&nbsp;array is empty.</li><li>From left to right read nums[i] and index[i], insert at index&nbsp;<code>index[i]</code>&nbsp;the value&nbsp;<code>nums[i]</code>&nbsp;in&nbsp;<em>target</em>&nbsp;array.</li><li>Repeat the previous step until there are no elements to read in&nbsp;<code>nums</code>&nbsp;and&nbsp;<code>index.</code></li></ul>



<p>Return the&nbsp;<em>target</em>&nbsp;array.</p>



<p>It is guaranteed that the insertion operations will be valid.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [0,1,2,3,4], index = [0,1,2,2,1]
<strong>Output:</strong> [0,4,1,3,2]
<strong>Explanation:</strong>
nums       index     target
0            0        [0]
1            1        [0,1]
2            2        [0,1,2]
3            2        [0,1,3,2]
4            1        [0,4,1,3,2]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,4,0], index = [0,1,2,3,0]
<strong>Output:</strong> [0,1,2,3,4]
<strong>Explanation:</strong>
nums       index     target
1            0        [1]
2            1        [1,2]
3            2        [1,2,3]
4            3        [1,2,3,4]
0            0        [0,1,2,3,4]
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1], index = [0]
<strong>Output:</strong> [1]
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= nums.length, index.length &lt;= 100</code></li><li><code>nums.length == index.length</code></li><li><code>0 &lt;= nums[i] &lt;= 100</code></li><li><code>0 &lt;= index[i] &lt;= i</code></li></ul>



<h2><strong>Solution: Simulation</strong></h2>



<p>Time complexity: O(n) ~ 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
class Solution {
public:
  vector&lt;int&gt; createTargetArray(vector&lt;int&gt;&amp; nums, vector&lt;int&gt;&amp; index) {
    vector&lt;int&gt; target;
    for (int i = 0; i &lt; nums.size(); ++i)
      target.insert(begin(target) + index[i], nums[i]);
    return target;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1389-create-target-array-in-the-given-order/">花花酱 LeetCode 1389. Create Target Array in the Given Order</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/simulation/leetcode-1389-create-target-array-in-the-given-order/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 701. Insert into a Binary Search Tree</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-701-insert-into-a-binary-search-tree/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-701-insert-into-a-binary-search-tree/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 13 Jul 2018 03:42:16 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[BST]]></category>
		<category><![CDATA[insert]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3099</guid>

					<description><![CDATA[<p>Problem Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-701-insert-into-a-binary-search-tree/">花花酱 LeetCode 701. Insert into a Binary Search Tree</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.</p>
<p>Note that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.</p>
<p>For example,</p><pre class="crayon-plain-tag">Given the tree:
        4
       / \
      2   7
     / \
    1   3
And the value to insert: 5</pre><p>You can return this binary search tree:</p><pre class="crayon-plain-tag">4
       /   \
      2     7
     / \   /
    1   3 5</pre><p>This tree is also valid:</p><pre class="crayon-plain-tag">5
       /   \
      2     7
     / \   
    1   3
         \
          4</pre><p></p>
<h1><strong>Solution: Recursion</strong></h1>
<p>Time complexity: O(logn ~ n)</p>
<p>Space complexity: O(logn ~ n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 52 ms
class Solution {
public:
  TreeNode* insertIntoBST(TreeNode* root, int val) {
    if (root == nullptr) return new TreeNode(val);
    if (val &gt; root-&gt;val)
      root-&gt;right = insertIntoBST(root-&gt;right, val);
    else
      root-&gt;left = insertIntoBST(root-&gt;left, val);
    return root;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-701-insert-into-a-binary-search-tree/">花花酱 LeetCode 701. Insert into a Binary Search Tree</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-701-insert-into-a-binary-search-tree/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
