<?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>inorder Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/inorder/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/inorder/</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>inorder Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/inorder/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1305. All Elements in Two Binary Search Trees</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-1305-all-elements-in-two-binary-search-trees/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-1305-all-elements-in-two-binary-search-trees/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 29 Dec 2019 19:05:06 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[BST]]></category>
		<category><![CDATA[inorder]]></category>
		<category><![CDATA[mergesort]]></category>
		<category><![CDATA[O(n)]]></category>
		<category><![CDATA[traversal]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6021</guid>

					<description><![CDATA[<p>Given two binary search trees&#160;root1&#160;and&#160;root2. Return a list containing&#160;all the integers&#160;from&#160;both trees&#160;sorted in&#160;ascending&#160;order. Example 1: Input: root1 = [2,1,4], root2 = [1,0,3] Output: [0,1,1,2,3,4] Example&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-1305-all-elements-in-two-binary-search-trees/">花花酱 LeetCode 1305. All Elements in Two Binary Search Trees</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1305. All Elements in Two Binary Search Trees - 刷题找工作 EP290" width="500" height="375" src="https://www.youtube.com/embed/2cbsWlAHlj4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given two binary search trees&nbsp;<code>root1</code>&nbsp;and&nbsp;<code>root2</code>.</p>



<p>Return a list containing&nbsp;<em>all the integers</em>&nbsp;from&nbsp;<em>both trees</em>&nbsp;sorted in&nbsp;<strong>ascending</strong>&nbsp;order.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" alt=""/></figure>



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



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> root1 = [0,-10,10], root2 = [5,1,7,0,2]
<strong>Output:</strong> [-10,0,0,1,2,5,7,10]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> root1 = [], root2 = [5,1,7,0,2]
<strong>Output:</strong> [0,1,2,5,7]
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> root1 = [0,-10,10], root2 = []
<strong>Output:</strong> [-10,0,10]
</pre>



<p><strong>Example 5:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> root1 = [1,null,8], root2 = [8,1]
<strong>Output:</strong> [1,1,8,8]
</pre>



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



<ul><li>Each tree has at most&nbsp;<code>5000</code>&nbsp;nodes.</li><li>Each node&#8217;s value is between&nbsp;<code>[-10^5, 10^5]</code>.</li></ul>



<h2><strong>Solution: Inorder traversal + Merge Sort</strong></h2>



<p>Time complexity: O(t1 + t2)<br>Space complexity: O(t1 + t2)</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; getAllElements(TreeNode* root1, TreeNode* root2) {    
    function&lt;void(TreeNode*, vector&lt;int&gt;&amp;)&gt; inorder = [&amp;](TreeNode* root, vector&lt;int&gt;&amp; t) {
      if (!root) return;
      inorder(root-&gt;left, t);
      t.push_back(root-&gt;val);
      inorder(root-&gt;right, t);
    };
    vector&lt;int&gt; t1;
    vector&lt;int&gt; t2;
    inorder(root1, t1);
    inorder(root2, t2);
    vector&lt;int&gt; m;
    int i = 0;
    int j = 0;
    while (m.size() != t1.size() + t2.size()) {
      if (j == t2.size()) m.push_back(t1[i++]);
      else if (i == t1.size()) m.push_back(t2[j++]);
      else m.push_back(t1[i] &lt; t2[j] ? t1[i++] : t2[j++]);      
    }
    return m;
  }
};</pre>
</div></div>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; getAllElements(TreeNode* root1, TreeNode* root2) {    
    function&lt;void(TreeNode*, vector&lt;int&gt;&amp;)&gt; inorder = [&amp;](TreeNode* root, vector&lt;int&gt;&amp; t) {
      if (!root) return;
      inorder(root-&gt;left, t);
      t.push_back(root-&gt;val);
      inorder(root-&gt;right, t);
    };
    vector&lt;int&gt; t1;
    vector&lt;int&gt; t2;
    inorder(root1, t1);
    inorder(root2, t2);
    vector&lt;int&gt; m;
    std::merge(begin(t1), end(t1), begin(t2), end(t2), back_inserter(m));    
    return m;
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-1305-all-elements-in-two-binary-search-trees/">花花酱 LeetCode 1305. All Elements in Two Binary Search Trees</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-1305-all-elements-in-two-binary-search-trees/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1273. Delete Tree Nodes</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-1273-delete-tree-nodes/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-1273-delete-tree-nodes/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 01 Dec 2019 05:25:33 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[inorder]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5897</guid>

					<description><![CDATA[<p>A tree rooted at node 0 is given as follows: The number of nodes is&#160;nodes; The value of the&#160;i-th node is&#160;value[i]; The parent of the&#160;i-th&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-1273-delete-tree-nodes/">花花酱 LeetCode 1273. Delete Tree Nodes</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>A tree rooted at node 0 is given as follows:</p>



<ul><li>The number of nodes is&nbsp;<code>nodes</code>;</li><li>The value of the&nbsp;<code>i</code>-th node is&nbsp;<code>value[i]</code>;</li><li>The parent of the&nbsp;<code>i</code>-th node is&nbsp;<code>parent[i]</code>.</li></ul>



<p>Remove every subtree whose sum of values of nodes is zero.</p>



<p>After doing so, return the number of nodes remaining in the tree.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/07/02/1421_sample_1.PNG" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nodes = 7, parent = [-1,0,0,1,2,2,2], value = [1,-2,4,0,-2,-1,-1]
<strong>Output:</strong> 2
</pre>



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



<ul><li><code>1 &lt;= nodes &lt;= 10^4</code></li><li><code>-10^5 &lt;= value[i] &lt;= 10^5</code></li><li><code>parent.length == nodes</code></li><li><code>parent[0] == -1</code>&nbsp;which indicates that&nbsp;<code>0</code>&nbsp;is the root.</li></ul>



<h2><strong>Solution: Inorder Traversal</strong></h2>



<p>For each node, return the sum of all its subtrees and number of nodes including itself after removal.</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:
  int deleteTreeNodes(int nodes, vector&lt;int&gt;&amp; parent, vector&lt;int&gt;&amp; value) {
    vector&lt;vector&lt;int&gt;&gt; g(nodes);
    for (int i = 1; i &lt; nodes; ++i)
      g[parent[i]].push_back(i);    
    // Returns &lt;sum, nodes&gt; of subtree n.
    function&lt;pair&lt;int,int&gt;(int)&gt; traverse = [&amp;](int n) {
      int sum = value[n];
      int count = 1;
      for (int x : g[n]) {
        auto [s, c] = traverse(x);        
        sum += s;
        count += s ? c : 0;
      }
      return make_pair(sum, sum ? count : 0);
    };    
    return traverse(0).second;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-1273-delete-tree-nodes/">花花酱 LeetCode 1273. Delete Tree Nodes</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-1273-delete-tree-nodes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 938. Range Sum of BST</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-938-range-sum-of-bst/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-938-range-sum-of-bst/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 11 Nov 2018 07:41:47 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[BST]]></category>
		<category><![CDATA[inorder]]></category>
		<category><![CDATA[range]]></category>
		<category><![CDATA[range sum]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4285</guid>

					<description><![CDATA[<p>Problem Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive). The binary search tree is guaranteed&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-938-range-sum-of-bst/">花花酱 LeetCode 938. Range Sum of BST</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 <code>root</code> node of a binary search tree, return the sum of values of all nodes with value between <code>L</code> and <code>R</code> (inclusive).</p>
<p>The binary search tree is guaranteed to have unique values.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>root = <span id="example-input-1-1">[10,5,15,3,7,null,18]</span>, L = <span id="example-input-1-2">7</span>, R = <span id="example-input-1-3">15</span>
<strong>Output: </strong><span id="example-output-1">32</span>
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>root = <span id="example-input-2-1">[10,5,15,3,7,13,18,1,null,6]</span>, L = <span id="example-input-2-2">6</span>, R = <span id="example-input-2-3">10</span>
<strong>Output: </strong><span id="example-output-2">23</span>
</pre>
<p><strong>Note:</strong></p>
<ol>
<li>The number of nodes in the tree is at most <code>10000</code>.</li>
<li>The final answer is guaranteed to be less than <code>2^31</code>.</li>
</ol>
<h1>Solution: In-order traversal</h1>
<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: 72 ms
class Solution {
public:
  int rangeSumBST(TreeNode* root, int L, int R) {
    if (!root) return 0;
    int sum = 0;
    if (root-&gt;val &gt;= L) sum += rangeSumBST(root-&gt;left, L, R);
    if (root-&gt;val &gt;= L &amp;&amp; root-&gt;val &lt;= R) sum += root-&gt;val;
    if (root-&gt;val &lt;= R) sum += rangeSumBST(root-&gt;right, L, R);
    return sum;
  }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-938-range-sum-of-bst/">花花酱 LeetCode 938. Range Sum of BST</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-938-range-sum-of-bst/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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>
		<item>
		<title>花花酱 LeetCode 538. Convert BST to Greater Tree</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-538-convert-bst-to-greater-tree/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-538-convert-bst-to-greater-tree/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 22 Mar 2018 04:19:55 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[BST]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[inorder]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2274</guid>

					<description><![CDATA[<p>Problem 题目大意：把二叉搜索树的每个节点加上比他大的节点的和。 https://leetcode.com/problems/convert-bst-to-greater-tree/description/ Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-538-convert-bst-to-greater-tree/">花花酱 LeetCode 538. Convert BST to Greater 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[<div class="question-description">
<h1><strong>Problem</strong></h1>
<p>题目大意：把二叉搜索树的每个节点加上比他大的节点的和。</p>
<p><a href="https://leetcode.com/problems/convert-bst-to-greater-tree/description/">https://leetcode.com/problems/convert-bst-to-greater-tree/description/</a></p>
<p>Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.</p>
<p><b>Example:</b></p>
<pre class="crayon:false"><b>Input:</b> The root of a Binary Search Tree like this:
              5
            /   \
           2     13

<b>Output:</b> The root of a Greater Tree like this:
             18
            /   \
          20     13
</pre>
</div>
<h1>Solution: reversed inorder traversal</h1>
<p>in a BST, we can visit every node in the decreasing order. Using a member sum to track the sum of all visited nodes.</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 56 ms
class Solution {
public:
  TreeNode* convertBST(TreeNode* root) {
    sum = 0;
    inorder(root);
    return root;
  }
private:
  int sum;
  void rinorder(TreeNode* root) {
    if (root == nullptr) return;
    rinorder(root-&gt;right);
    root-&gt;val = (sum += root-&gt;val);
    rinorder(root-&gt;left);
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-538-convert-bst-to-greater-tree/">花花酱 LeetCode 538. Convert BST to Greater 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-538-convert-bst-to-greater-tree/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 515. Find Largest Value in Each Tree Row</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-515-find-largest-value-in-each-tree-row/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-515-find-largest-value-in-each-tree-row/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 06 Mar 2018 05:08:33 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[inorder]]></category>
		<category><![CDATA[max element]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[travesal]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1982</guid>

					<description><![CDATA[<p>题目大意：给你一棵树，返回每一层最大的元素的值。 You need to find the largest value in each row of a binary tree. Example: [crayon-663a264003f68846436844/] Solution 1: Inorder traversal + depth info C++&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-515-find-largest-value-in-each-tree-row/">花花酱 LeetCode 515. Find Largest Value in Each Tree Row</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="question-description">
<p>题目大意：给你一棵树，返回每一层最大的元素的值。</p>
<p>You need to find the largest value in each row of a binary tree.</p>
<p><b>Example:</b></p><pre class="crayon-plain-tag">&lt;b&gt;Input:&lt;/b&gt; 

          1
         / \
        3   2
       / \   \  
      5   3   9 

&lt;b&gt;Output:&lt;/b&gt; [1, 3, 9]</pre><p>
</div>
<p>Solution 1: Inorder traversal + depth info</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 15 ms
class Solution {
public:
  vector&lt;int&gt; largestValues(TreeNode* root) {
    vector&lt;int&gt; ans;
    inorder(root, 0, ans);
    return ans;
  }
private:
  void inorder(TreeNode* root, int d, vector&lt;int&gt;&amp; ans) {
    if (root == nullptr) return;
    while (ans.size() &lt;= d) ans.push_back(INT_MIN);    
    inorder(root-&gt;left, d + 1, ans);
    ans[d] = max(ans[d], root-&gt;val);
    inorder(root-&gt;right, d + 1, ans);
  }
};</pre><p>Python3</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 72 ms (beats 100%)
"""
class Solution:
  def largestValues(self, root):
    def inorder(root, d, ans):
      if not root: return ans
      if len(ans) &lt;= d: ans += [float('-inf')]
      inorder(root.left, d + 1, ans)
      if root.val &gt; ans[d]: ans[d] = root.val
      inorder(root.right, d + 1, ans)
      return ans
    
    return inorder(root, 0, [])</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-515-find-largest-value-in-each-tree-row/">花花酱 LeetCode 515. Find Largest Value in Each Tree Row</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-515-find-largest-value-in-each-tree-row/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 513. Find Bottom Left Tree Value</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-513-find-bottom-left-tree-value/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-513-find-bottom-left-tree-value/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 06 Mar 2018 04:55:25 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[inorder]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[traversal]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1978</guid>

					<description><![CDATA[<p>题目大意：给你一棵树，返回左下角（最后一行最左面）的节点的值。 https://leetcode.com/problems/find-bottom-left-tree-value/description/ Problem: Given a binary tree, find the leftmost value in the last row of the tree. Example 1: [crayon-663a26400408c098349092/] Example 2:  [crayon-663a26400408e095296946/] Note: You&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-513-find-bottom-left-tree-value/">花花酱 LeetCode 513. Find Bottom Left Tree Value</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>题目大意：给你一棵树，返回左下角（最后一行最左面）的节点的值。</p>
<p><a href="https://leetcode.com/problems/find-bottom-left-tree-value/description/">https://leetcode.com/problems/find-bottom-left-tree-value/description/</a></p>
<p><strong>Problem:</strong></p>
<p>Given a binary tree, find the leftmost value in the last row of the tree.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input:

    2
   / \
  1   3

Output:
1</pre><p><b>Example 2: </b></p><pre class="crayon-plain-tag">Input:

        1
       / \
      2   3
     /   / \
    4   5   6
       /
      7

Output:
7</pre><p><b>Note:</b> You may assume the tree (i.e., the given root node) is not <b>NULL</b>.</p>
<p><strong>Solution 1: Inorder traversal with depth info</strong></p>
<p>The first node visited in the deepest row is the answer.</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 12 ms
class Solution {
public:
  int findBottomLeftValue(TreeNode* root) {
    int max_row = -1;    
    int ans;
    dfs(root, 0, max_row, ans);
    return ans;
  }
private:
  void inorder(TreeNode* root, int row, int&amp; max_row, int&amp; ans) {
    if (root == nullptr) return;
    inorder(root-&gt;left, row + 1, max_row, ans);
    if (row &gt; max_row) {
      ans = root-&gt;val;
      max_row = row;      
    }
    inorder(root-&gt;right, row + 1, max_row, ans);
  }
};</pre><p>Python3</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 92 ms
"""
class Solution:
  def inorder(self, root, row):
    if not root: return
    self.inorder(root.left, row + 1)
    if row &gt; self.max_row:
      self.max_row, self.ans = row, root.val
    self.inorder(root.right, row + 1)    
      
  def findBottomLeftValue(self, root):        
    self.ans, self.max_row = 0, -1
    self.inorder(root, 0)
    return self.ans</pre><p>&nbsp;</p>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-513-find-bottom-left-tree-value/">花花酱 LeetCode 513. Find Bottom Left Tree Value</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-513-find-bottom-left-tree-value/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 145. Binary Tree Postorder Traversal</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-145-binary-tree-postorder-traversal/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-145-binary-tree-postorder-traversal/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 08 Sep 2017 08:19:18 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[binary tree]]></category>
		<category><![CDATA[inorder]]></category>
		<category><![CDATA[iterative]]></category>
		<category><![CDATA[leetcode]]></category>
		<category><![CDATA[postorder]]></category>
		<category><![CDATA[preorder]]></category>
		<category><![CDATA[recursive]]></category>
		<category><![CDATA[solutions]]></category>
		<category><![CDATA[traversal]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=150</guid>

					<description><![CDATA[<p>Problem: Given a binary tree, return the postorder traversal of its nodes&#8217; values. For example: Given binary tree {1,#,2,3}, [crayon-663a26400418a055223064/] return [3,2,1]. Note: Recursive solution is trivial, could you do&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-145-binary-tree-postorder-traversal/">花花酱 LeetCode 145. Binary Tree Postorder 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><iframe width="500" height="375" src="https://www.youtube.com/embed/A6iCX_5xiU4?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem:</strong></h1>
<p>Given a binary tree, return the <i>postorder</i> traversal of its nodes&#8217; values.</p>
<p>For example:<br />
Given binary tree <code>{1,#,2,3}</code>,</p><pre class="crayon-plain-tag">1
    \
     2
    /
   3</pre><p>return <code>[3,2,1]</code>.</p>
<p><b>Note:</b> Recursive solution is trivial, could you do it iteratively?</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/145-ep40-1.png"><img class="alignnone size-full wp-image-154" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/145-ep40-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/145-ep40-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/145-ep40-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/145-ep40-1-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/145-ep40-1-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/145-ep40-2.png"><img class="alignnone size-full wp-image-153" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/145-ep40-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/145-ep40-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/145-ep40-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/145-ep40-2-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/145-ep40-2-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<h1><strong>Solution 1:</strong></h1>
<p></p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
    vector&lt;int&gt; postorderTraversal(TreeNode* root) {
        vector&lt;int&gt; ans;        
        postorderTraversal(root, ans);
        return ans;
    }
    
    void postorderTraversal(TreeNode* root, vector&lt;int&gt;&amp; ans) {
        if (!root) return;
        postorderTraversal(root-&gt;left, ans);
        postorderTraversal(root-&gt;right, ans);
        ans.push_back(root-&gt;val);
    }
};</pre><p></p>
<h1><strong>Solution 2:</strong></h1>
<p></p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
    vector&lt;int&gt; postorderTraversal(TreeNode* root) {
        if (!root) return {};
        vector&lt;int&gt; ans;
        const vector&lt;int&gt; l = postorderTraversal(root-&gt;left);
        const vector&lt;int&gt; r = postorderTraversal(root-&gt;right);
        ans.insert(ans.end(), l.begin(), l.end());
        ans.insert(ans.end(), r.begin(), r.end());
        ans.push_back(root-&gt;val);
        return ans;
    }
};</pre><p></p>
<h1><strong>Solution 3:</strong></h1>
<p></p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
    vector&lt;int&gt; postorderTraversal(TreeNode* root) {
        if (!root) return {};
        deque&lt;int&gt; ans;
        stack&lt;TreeNode*&gt; s;
        s.push(root);
        while (!s.empty()) {
            TreeNode* n = s.top();
            s.pop();
            ans.push_front(n-&gt;val); // O(1)
            if (n-&gt;left) s.push(n-&gt;left);
            if (n-&gt;right) s.push(n-&gt;right);
        }   
        return vector&lt;int&gt;(ans.begin(), ans.end());
    }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-145-binary-tree-postorder-traversal/">花花酱 LeetCode 145. Binary Tree Postorder 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-145-binary-tree-postorder-traversal/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
