<?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>pointers Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/pointers/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/pointers/</link>
	<description></description>
	<lastBuildDate>Tue, 20 Mar 2018 03:58: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>pointers Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/pointers/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 623. Add One Row to Tree</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-623-add-one-row-to-tree/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-623-add-one-row-to-tree/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 20 Mar 2018 03:57:05 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[pointers]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2228</guid>

					<description><![CDATA[<p>题目大意：在树的所有第d层位置插入元素v。 Problem https://leetcode.com/problems/add-one-row-to-tree/description/ Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with value v at the given&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-623-add-one-row-to-tree/">花花酱 LeetCode 623. Add One Row to 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[<p>题目大意：在树的所有第d层位置插入元素v。</p>
<h1><strong>Problem</strong></h1>
<p><a href="https://leetcode.com/problems/add-one-row-to-tree/description/">https://leetcode.com/problems/add-one-row-to-tree/description/</a></p>
<p>Given the root of a binary tree, then value <code>v</code> and depth <code>d</code>, you need to add a row of nodes with value <code>v</code> at the given depth <code>d</code>. The root node is at depth 1.</p>
<p>The adding rule is: given a positive integer depth <code>d</code>, for each NOT null tree nodes <code>N</code> in depth <code>d-1</code>, create two tree nodes with value <code>v</code> as <code>N's</code> left subtree root and right subtree root. And <code>N's</code> <b>original left subtree</b> should be the left subtree of the new left subtree root, its <b>original right subtree</b> should be the right subtree of the new right subtree root. If depth <code>d</code> is 1 that means there is no depth d-1 at all, then create a tree node with value <b>v</b> as the new root of the whole original tree, and the original tree is the new root&#8217;s left subtree.</p>
<p><b>Example 1:</b></p>
<pre class="crayon:false"><b>Input:</b> 
A binary tree as following:
       4
     /   \
    2     6
   / \   / 
  3   1 5   

<b>v = 1</b>

<b>d = 2</b>

<b>Output:</b> 
       4
      / \
     1   1
    /     \
   2       6
  / \     / 
 3   1   5   

</pre>
<p><b>Example 2:</b></p>
<pre class="crayon:false"><b>Input:</b> 
A binary tree as following:
      4
     /   
    2    
   / \   
  3   1    

<b>v = 1</b>

<b>d = 3</b>

<b>Output:</b> 
      4
     /   
    2
   / \    
  1   1
 /     \  
3       1
</pre>
<p><b>Note:</b></p>
<ol>
<li>The given d is in range [1, maximum depth of the given tree + 1].</li>
<li>The given binary tree has at least one tree node.</li>
</ol>
<h1><strong>Solution: Recursion</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 19 ms (beats 90.85%)
class Solution {
public:
  TreeNode* addOneRow(TreeNode* root, int v, int d) {
    if (root == nullptr) return nullptr;
    
    // Special case.
    if (d == 1) {
      TreeNode* new_root = new TreeNode(v);
      new_root-&gt;left = root;
      return new_root;
    }
    
    if (d == 2) {
      TreeNode* l = root-&gt;left;
      root-&gt;left = new TreeNode(v);
      root-&gt;left-&gt;left = l;
      
      TreeNode* r = root-&gt;right;
      root-&gt;right = new TreeNode(v);
      root-&gt;right-&gt;right = r;
    } else {
      addOneRow(root-&gt;left, v, d - 1);
      addOneRow(root-&gt;right, v, d - 1);
    }
    
    return root;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-623-add-one-row-to-tree/">花花酱 LeetCode 623. Add One Row to 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-623-add-one-row-to-tree/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
