<?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>binary search tree Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/binary-search-tree/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/binary-search-tree/</link>
	<description></description>
	<lastBuildDate>Wed, 11 Jul 2018 01:58:15 +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>binary search tree Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/binary-search-tree/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 669. Trim a Binary Search Tree</title>
		<link>https://zxi.mytechroad.com/blog/leetcode/leetcode-669-trim-a-binary-search-tree/</link>
					<comments>https://zxi.mytechroad.com/blog/leetcode/leetcode-669-trim-a-binary-search-tree/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 05 Sep 2017 01:13:36 +0000</pubDate>
				<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[Tree]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[binary tree]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[memory leak]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[trim]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=90</guid>

					<description><![CDATA[<p>Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R &#62;= L).&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-669-trim-a-binary-search-tree/">花花酱 LeetCode 669. Trim 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[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/L_t2x3nH61k?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p>Given a binary search tree and the lowest and highest boundaries as <code>L</code> and <code>R</code>, trim the tree so that all its elements lies in <code>[L, R]</code> (R &gt;= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input:
    1
   / \
  0   2

  L = 1
  R = 2

Output: 
    1
      \
       2</pre><p><b>Example 2:</b></p><pre class="crayon-plain-tag">Input:
    3
   / \
  0   4
   \
    2
   /
  1

  L = 1
  R = 3

Output:
      3
     / 
   2   
  /
 1</pre><p>This problem can be solved with recursion</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/699-1-1.png"><img class="alignnone wp-image-100 size-full" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/699-1-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/699-1-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/699-1-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/699-1-1-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/699-1-1-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p>There 3 cases in total depends on the root value and L, R</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p>Solution:</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
    // No cleanup -&gt; memory leak 
    TreeNode* trimBST(TreeNode* root, int L, int R) {
        if(!root) return nullptr;
        // val not in range, return the left/right subtrees
        if(root-&gt;val &lt; L) return trimBST(root-&gt;right, L, R);
        if(root-&gt;val &gt; R) return trimBST(root-&gt;left, L, R);
        // val in [L, R], recusively trim left/right subtrees
        root-&gt;left = trimBST(root-&gt;left, L, R);
        root-&gt;right = trimBST(root-&gt;right, L, R);
        return root;
    }
};</pre><p>The previous solution has potential memory leak for languages without garbage collection.</p>
<p>Here&#8217;s the full program to delete trimmed nodes.</p><pre class="crayon-plain-tag">// Author: Huahua
#include &lt;iostream&gt;
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
 
class Solution {
public:
    
    // With cleanup -&gt; no memory leak
    TreeNode*&amp; trimBST(TreeNode*&amp; root, int L, int R) {
        if(!root) return root;
        
        if(root-&gt;val &lt; L) {            
            auto&amp; result = trimBST(root-&gt;right, L, R);
            deleteTree(root-&gt;left);
            delete root;
            root=nullptr;
            return result;
        } else if(root-&gt;val &gt; R) {
            auto&amp; result = trimBST(root-&gt;left, L, R);
            deleteTree(root-&gt;right);
            delete root;
            root=nullptr;
            return result;
        } else {
            // recusively trim left/right subtrees
            root-&gt;left = trimBST(root-&gt;left, L, R);
            root-&gt;right = trimBST(root-&gt;right, L, R);
            return root;
        }
    }
    
    void deleteTree(TreeNode* &amp;root) {
        if(!root) return;
        deleteTree(root-&gt;left);
        deleteTree(root-&gt;right);
        delete root;
        root=nullptr;
    }
};

void PrintTree(TreeNode* root) {
    if(!root) {
        cout&lt;&lt;"null ";
        return;
    };
    if(!root-&gt;left &amp;&amp; !root-&gt;right) {
        cout&lt;&lt;root-&gt;val&lt;&lt;" ";
    } else {
        cout&lt;&lt;root-&gt;val&lt;&lt;" ";
        PrintTree(root-&gt;left);
        PrintTree(root-&gt;right);
    }
}


int main()
{
    TreeNode* root=new TreeNode(2);
    root-&gt;left=new TreeNode(1);
    root-&gt;right=new TreeNode(3);
    PrintTree(root);
    std::cout&lt;&lt;std::endl;
    
    TreeNode* t = Solution().trimBST(root, 3, 4);
    PrintTree(t);
    std::cout&lt;&lt;std::endl;

    // Original root was deleted
    PrintTree(root);
    std::cout&lt;&lt;std::endl;
    
    return 0;
}</pre><p>Example output</p><pre class="crayon-plain-tag">2 1 3 
3 
null</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-669-trim-a-binary-search-tree/">花花酱 LeetCode 669. Trim 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/leetcode/leetcode-669-trim-a-binary-search-tree/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
