<?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>modify Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/modify/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/modify/</link>
	<description></description>
	<lastBuildDate>Wed, 12 Feb 2020 06:50:17 +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>modify Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/modify/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 897. Increasing Order Search Tree</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-897-increasing-order-search-tree/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-897-increasing-order-search-tree/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 02 Sep 2018 05:42:34 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[inorder]]></category>
		<category><![CDATA[modify]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3801</guid>

					<description><![CDATA[<p>Problem Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-897-increasing-order-search-tree/">花花酱 LeetCode 897. Increasing Order 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 a tree, rearrange the tree in <strong>in-order</strong> so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child.</p>
<pre class="crayon:false"><strong>Example 1:</strong>
<strong>Input:</strong> [5,3,6,2,4,null,8,1,null,null,null,7,9]

       5
      / \
    3    6
   / \    \
  2   4    8
 /        / \ 
1        7   9

<strong>Output:</strong> [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]

 1
  \
   2
    \
     3
      \
       4
        \
         5
          \
           6
            \
             7
              \
               8
                \
                 9</pre>
<p><strong>Note:</strong></p>
<ol>
<li>The number of nodes in the given tree will be between 1 and 100.</li>
<li>Each node will have a unique integer value from 0 to 1000.</li>
</ol>
<h1><strong>Solution: In-order traversal</strong></h1>
<div>root = 5</div>
<div>inorder(root.left) 之后</div>
<div>self.prev = 4</div>
<div>（1-4）已经处理完了，这时候的树是很奇怪的一个形状，<wbr />3即是2的右子树，又是5的左子树。</div>
<div><span style="font-family: monospace;">   1<br />
</span></div>
<div>
<div><span style="font-family: monospace;">    \</span></div>
<div><span style="font-family: monospace;">     2    5</span></div>
<div><span style="font-family: monospace;">      \  /  \ </span></div>
<div><span style="font-family: monospace;">       3     6</span></div>
<div><span style="font-family: monospace;">        \     \</span></div>
<div><span style="font-family: monospace;"><b><span style="color: #ff0000;"> prev -&gt;</span></b> <b><span style="color: #ff0000;">4</span></b>     8</span></div>
<div><span style="font-family: monospace;">             /  \</span></div>
<div><span style="font-family: monospace;">            7    9</span></div>
<div>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</div>
<div></div>
</div>
<div>5.left = None # 把5-&gt;3的链接断开</div>
<div><span style="font-family: monospace;">5</span></div>
<div><span style="font-family: monospace;"> \</span></div>
<div><span style="font-family: monospace;">  6</span></div>
<div><span style="font-family: monospace;">   \</span></div>
<div><span style="font-family: monospace;">    8</span></div>
<div><span style="font-family: monospace;">   /  \</span></div>
<div><span style="font-family: monospace;">  7    9</span></div>
<div>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</div>
<div>self.prev.right = root  &lt;=&gt; 4.right = 5</div>
<div>把5接到4的右子树</div>
<div><span style="font-family: monospace;">1</span></div>
<div><span style="font-family: monospace;"> \</span></div>
<div><span style="font-family: monospace;">  2</span></div>
<div><span style="font-family: monospace;">   \</span></div>
<div><span style="font-family: monospace;">    3</span></div>
<div><span style="font-family: monospace;">     \</span></div>
<div><span style="font-family: monospace;">      4</span></div>
<div><span style="font-family: monospace;">       \</span></div>
<div><span style="font-family: monospace;">        <span style="color: #ff0000;"><b>5 &lt;&#8211; prev</b></span></span></div>
<div><span style="font-family: monospace;">         \</span></div>
<div><span style="font-family: monospace;">          <span style="color: #0000ff;">6</span></span></div>
<div><span style="color: #0000ff; font-family: monospace;">           \</span></div>
<div><span style="color: #0000ff; font-family: monospace;">            8</span></div>
<div><span style="color: #0000ff; font-family: monospace;">          /   \</span></div>
<div><span style="color: #0000ff; font-family: monospace;">         7     9</span></div>
<div><span style="font-family: monospace;">self.prev = root &lt;=&gt; prev = 5</span></div>
<div>inorder(5.right) &lt;=&gt; inorder(6) 然后再去递归处理6（及其子树）即可。</div>
<p>Time complexity: O(n)</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
// Running time: 64 ms
class Solution {
public:
  TreeNode* increasingBST(TreeNode* root) {
    TreeNode dummy(0);
    prev_ = &amp;dummy;
    inorder(root);
    return dummy.right;
  }
private:  
  TreeNode* prev_;
  void inorder(TreeNode* root) {
    if (root == nullptr) return;
    inorder(root-&gt;left);
    prev_-&gt;right = root;  
    prev_ = root;
    prev_-&gt;left = nullptr;
    inorder(root-&gt;right);
  }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">class Solution {
  private TreeNode prev;
  
  public TreeNode increasingBST(TreeNode root) {
    TreeNode dummy = new TreeNode(0);
    this.prev = dummy;
    inorder(root);
    return dummy.right;
  }
  
  private void inorder(TreeNode root) {
    if (root == null) return;
    inorder(root.left);
    this.prev.right = root;
    this.prev = root;
    this.prev.left = null;
    inorder(root.right);
  }
}</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">class Solution:
  def increasingBST(self, root):
    dummy = TreeNode(0)
    self.prev = dummy
    def inorder(root):
      if not root: return None
      inorder(root.left)
      root.left = None
      self.prev.right = root
      self.prev = root
      inorder(root.right)
    inorder(root)
    return dummy.right</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-897-increasing-order-search-tree/">花花酱 LeetCode 897. Increasing Order 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-897-increasing-order-search-tree/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
