<?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>inserter Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/inserter/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/inserter/</link>
	<description></description>
	<lastBuildDate>Sun, 07 Oct 2018 04:58:37 +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>inserter Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/inserter/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 919. Complete Binary Tree Inserter</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-919-complete-binary-tree-inserter/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-919-complete-binary-tree-inserter/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 07 Oct 2018 04:29:53 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[deque]]></category>
		<category><![CDATA[inserter]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4163</guid>

					<description><![CDATA[<p>Problem A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-919-complete-binary-tree-inserter/">花花酱 LeetCode 919. Complete Binary Tree Inserter</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>A <em>complete</em> binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.</p>
<p>Write a data structure <code>CBTInserter</code> that is initialized with a complete binary tree and supports the following operations:</p>
<ul>
<li><code>CBTInserter(TreeNode root)</code> initializes the data structure on a given tree with head node <code>root</code>;</li>
<li><code>CBTInserter.insert(int v)</code> will insert a <code>TreeNode</code> into the tree with value <code>node.val = v</code> so that the tree remains complete, <strong>and returns the value of the parent of the inserted <code>TreeNode</code></strong>;</li>
<li><code>CBTInserter.get_root()</code> will return the head node of the tree.</li>
</ul>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>inputs = <span id="example-input-1-1">["CBTInserter","insert","get_root"]</span>, inputs = <span id="example-input-1-2">[[[1]],[2],[]]</span>
<strong>Output: </strong><span id="example-output-1">[null,1,[1,2]]</span>
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>inputs = <span id="example-input-2-1">["CBTInserter","insert","insert","get_root"]</span>, inputs = <span id="example-input-2-2">[[[1,2,3,4,5,6]],[7],[8],[]]</span>
<strong>Output: </strong><span id="example-output-2">[null,3,4,[1,2,3,4,5,6,7,8]]</span></pre>
<p><strong>Note:</strong></p>
<ol>
<li>The initial given tree is complete and contains between <code>1</code> and <code>1000</code> nodes.</li>
<li><code>CBTInserter.insert</code> is called at most <code>10000</code> times per test case.</li>
<li>Every value of a given or inserted node is between <code>0</code> and <code>5000</code>.</li>
</ol>
<h1><strong>Solution 2: Deque</strong></h1>
<p>Using a deck to keep track of insertable nodes (potential parents) in order.</p>
<p>Time complexity: O(1) / O(n) first call</p>
<p>Space complexity: O(n)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class CBTInserter {
public:
  CBTInserter(TreeNode* root): root_(root), d_({root}) {}

  int insert(int v) {    
    while (!d_.empty()) {
      TreeNode* r = d_.front();
      if (r-&gt;left &amp;&amp; r-&gt;right) {
        d_.push_back(r-&gt;left);
        d_.push_back(r-&gt;right);
        d_.pop_front();
      } else if (r-&gt;left) {
        r-&gt;right = new TreeNode(v);
        return r-&gt;val;
      } else {
        r-&gt;left = new TreeNode(v);        
        return r-&gt;val;
      }
    }
  }

  TreeNode* get_root() { return root_; }
private:
  std::deque&lt;TreeNode*&gt; d_;
  TreeNode* root_;
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-919-complete-binary-tree-inserter/">花花酱 LeetCode 919. Complete Binary Tree Inserter</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-919-complete-binary-tree-inserter/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
