<?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>delete Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/delete/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/delete/</link>
	<description></description>
	<lastBuildDate>Sat, 10 Aug 2019 17:47:47 +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>delete Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/delete/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1110. Delete Nodes And Return Forest</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-1110-delete-nodes-and-return-forest/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-1110-delete-nodes-and-return-forest/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 09 Aug 2019 06:55:02 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5402</guid>

					<description><![CDATA[<p>Given the&#160;root&#160;of a binary tree, each node in the tree has a distinct value. After deleting&#160;all nodes with a value in&#160;to_delete, we are left with&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-1110-delete-nodes-and-return-forest/">花花酱 LeetCode 1110. Delete Nodes And Return Forest</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 width="500" height="375" src="https://www.youtube.com/embed/SEW3Vofoj_k?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given the&nbsp;<code>root</code>&nbsp;of a binary tree, each node in the tree has a distinct value.</p>



<p>After deleting&nbsp;all nodes with a value in&nbsp;<code>to_delete</code>, we are left with a forest (a&nbsp;disjoint union of trees).</p>



<p>Return the roots of the trees in the remaining forest.&nbsp; You may return the result in any order.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/07/01/screen-shot-2019-07-01-at-53836-pm.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> root = [1,2,3,4,5,6,7], to_delete = [3,5]
<strong>Output:</strong> [[1,2,null,4],[6],[7]]
</pre>



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



<ul><li>The number of nodes in the given tree is at most&nbsp;<code>1000</code>.</li><li>Each node has a distinct value between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>1000</code>.</li><li><code>to_delete.length &lt;= 1000</code></li><li><code>to_delete</code>&nbsp;contains distinct values between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>1000</code>.</li></ul>



<h2><strong>Solution: Recursion / Post-order traversal </strong></h2>



<figure class="wp-block-image"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2019/08/1110-ep262.png" alt="" class="wp-image-5406" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2019/08/1110-ep262.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2019/08/1110-ep262-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2019/08/1110-ep262-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p>Recursively delete nodes on left subtree and right subtree and return the trimmed tree.<br>if the current node needs to be deleted, then its non-null children will be added to output array.</p>



<p>Time complexity: O(n)<br>Space complexity: O(|d| + h)</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;TreeNode*&gt; delNodes(TreeNode* root, vector&lt;int&gt;&amp; to_delete) {
    vector&lt;TreeNode*&gt; ans;
    unordered_set&lt;int&gt; s{begin(to_delete), end(to_delete)};
    function&lt;TreeNode*(TreeNode*)&gt; del = [&amp;](TreeNode* n) -&gt; TreeNode* {
      if (!n) return nullptr;
      if (n-&gt;left) n-&gt;left = del(n-&gt;left);
      if (n-&gt;right) n-&gt;right = del(n-&gt;right);
      if (!s.count(n-&gt;val)) return n;
      if (n-&gt;left) ans.push_back(n-&gt;left);
      if (n-&gt;right) ans.push_back(n-&gt;right);      
      return nullptr;
    };
    TreeNode* r = del(root);
    if (r) ans.push_back(r);
    return ans;
  }
};</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def delNodes(self, root: TreeNode, to_delete: List[int]) -&gt; List[TreeNode]:
    ans = []
    ds = set(to_delete)
    def process(n):
      if not n: return None
      n.left, n.right = process(n.left), process(n.right)
      if n.val not in ds: return n
      if n.left: ans.append(n.left)
      if n.right: ans.append(n.right)
      return None
    root = process(root)
    if root: ans.append(root)
    return ans</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-1110-delete-nodes-and-return-forest/">花花酱 LeetCode 1110. Delete Nodes And Return Forest</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-1110-delete-nodes-and-return-forest/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 712. Minimum ASCII Delete Sum for Two Strings</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-712-minimum-ascii-delete-sum-for-two-strings/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-712-minimum-ascii-delete-sum-for-two-strings/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 21 Jul 2018 06:51:23 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[ascii]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3236</guid>

					<description><![CDATA[<p>Problem Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. Example 1: Input: s1 = "sea", s2&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-712-minimum-ascii-delete-sum-for-two-strings/">花花酱 LeetCode 712. Minimum ASCII Delete Sum for Two Strings</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/RFBWavPKCJg?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>Given two strings <code>s1, s2</code>, find the lowest ASCII sum of deleted characters to make two strings equal.</p>
<p><b>Example 1:</b></p>
<pre class="crayon:false"><b>Input:</b> s1 = "sea", s2 = "eat"
<b>Output:</b> 231
<b>Explanation:</b> Deleting "s" from "sea" adds the ASCII value of "s" (115) to the sum.
Deleting "t" from "eat" adds 116 to the sum.
At the end, both strings are equal, and 115 + 116 = 231 is the minimum sum possible to achieve this.
</pre>
<p><b>Example 2:</b></p>
<pre class="crayon:false"><b>Input:</b> s1 = "delete", s2 = "leet"
<b>Output:</b> 403
<b>Explanation:</b> Deleting "dee" from "delete" to turn the string into "let",
adds 100[d]+101[e]+101[e] to the sum.  Deleting "e" from "leet" adds 101[e] to the sum.
At the end, both strings are equal to "let", and the answer is 100+101+101+101 = 403.
If instead we turned both strings into "lee" or "eet", we would get answers of 433 or 417, which are higher.
</pre>
<p><b>Note:</b></p>
<ul>
<li><code>0 &lt; s1.length, s2.length &lt;= 1000</code>.</li>
<li>All elements of each string will have an ASCII value in <code>[97, 122]</code>.</li>
</ul>
<h1><strong>Solution: DP</strong></h1>
<p>Time complexity: O(l1 * l2)</p>
<p>Space complexity: O(l1 * l2)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 8 ms
class Solution {
public:
  int minimumDeleteSum(string s1, string s2) {
    const int l1 = s1.length();
    const int l2 = s2.length();
    // dp[i][j] := min delete sum of (s1.substr(0, i), s2.substr(0, j))
    vector&lt;vector&lt;int&gt;&gt; dp(l1 + 1, vector&lt;int&gt;(l2 + 1));
    for (int i = 1; i &lt;= l1; ++i)
      dp[i][0] = dp[i - 1][0] + s1[i - 1];
    for (int j = 1; j &lt;= l2; ++j)
      dp[0][j] = dp[0][j - 1] + s2[j - 1];    
    for (int i = 1; i &lt;= l1; ++i)
      for (int j = 1; j &lt;= l2; ++j)
        if (s1[i - 1] == s2[j - 1])
          // keep s1[i - 1] and s2[j - 1]
          dp[i][j] = dp[i - 1][j - 1]; 
        else
          dp[i][j] = min(dp[i - 1][j] + s1[i - 1],  // delete s1[i - 1]
                         dp[i][j - 1] + s2[j - 1]); // delete s2[j - 1]
    return dp[l1][l2];
  }
};</pre><p></p>
<h1><strong>Solution2: Recursion + Memorization</strong></h1>
<p>Time complexity: O(l1 * l2)</p>
<p>Space complexity: O(l1 * l2)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 20 ms
class Solution {
public:
  int minimumDeleteSum(string s1, string s2) {
    const int l1 = s1.length();
    const int l2 = s2.length();
    m_ = vector&lt;vector&lt;int&gt;&gt;(l1 + 1, vector&lt;int&gt;(l2 + 1, INT_MAX));
    return dp(s1, l1, s2, l2);
  }
private:
  int dp(const string&amp; s1, int i, const string&amp; s2, int j) {
    if (i == 0 &amp;&amp; j == 0) return 0;
    if (m_[i][j] != INT_MAX) return m_[i][j];
    if (i == 0) // s1 is empty.
      return m_[i][j] = dp(s1, i, s2, j - 1) + s2[j - 1];
    if (j == 0) // s2 is empty
      return m_[i][j] = dp(s1, i - 1, s2, j) + s1[i - 1];
    if (s1[i - 1] == s2[j - 1]) // skip s1[i-1] / s2[j-1]
      return m_[i][j] = dp(s1, i - 1, s2, j - 1);
    return m_[i][j] = min(dp(s1, i - 1, s2, j) + s1[i - 1],  // del s1[i-1]
                          dp(s1, i, s2, j - 1) + s2[j - 1]); // del s2[j-1]
  }  
  vector&lt;vector&lt;int&gt;&gt; m_;
};</pre><p></p>
<h1><strong>Related Problems</strong></h1>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-72-edit-distance/">花花酱 LeetCode 72. Edit Distance</a></li>
<li><a href="http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-115-distinct-subsequences/">花花酱 LeetCode 115. Distinct Subsequences</a></li>
</ul>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-712-minimum-ascii-delete-sum-for-two-strings/">花花酱 LeetCode 712. Minimum ASCII Delete Sum for Two Strings</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/dynamic-programming/leetcode-712-minimum-ascii-delete-sum-for-two-strings/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 450. Delete Node in a BST</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-450-delete-node-in-a-bst/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-450-delete-node-in-a-bst/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 24 Mar 2018 20:36:48 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[BST]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2358</guid>

					<description><![CDATA[<p>Problem https://leetcode.com/problems/delete-node-in-a-bst/description/ Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-450-delete-node-in-a-bst/">花花酱 LeetCode 450. Delete Node in a 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[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/00r9qf7lgAk?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1>Problem</h1>
<p><a href="https://leetcode.com/problems/delete-node-in-a-bst/description/">https://leetcode.com/problems/delete-node-in-a-bst/description/</a></p>
<div class="question-description">
<div>
<p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p><b>Note:</b> Time complexity should be O(height of tree).</p>
<p><b>Example:</b></p>
<pre class="crayon:false ">root = [5,3,6,2,4,null,7]
key = 3

    5
   / \
  3   6
 / \   \
2   4   7

Given key to delete is 3. So we find the node with value 3 and delete it.

One valid answer is [5,4,6,2,null,null,7], shown in the following BST.

    5
   / \
  4   6
 /     \
2       7

Another valid answer is [5,2,6,null,4,null,7].

    5
   / \
  2   6
   \   \
    4   7</pre>
</div>
</div>
<h1><img class="alignnone size-full wp-image-2403" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/03/450-ep177-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/03/450-ep177-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/03/450-ep177-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/03/450-ep177-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></h1>
<h1><img class="alignnone size-full wp-image-2402" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/03/450-ep177-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/03/450-ep177-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/03/450-ep177-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/03/450-ep177-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></h1>
<h1><img class="alignnone size-full wp-image-2401" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/03/450-ep177-3.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/03/450-ep177-3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/03/450-ep177-3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/03/450-ep177-3-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></h1>
<h1><strong>Solution: Recursion</strong></h1>
<p>Time complexity: O(h)</p>
<p>Space complexity: O(h)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 36 ms
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
  TreeNode* deleteNode(TreeNode* root, int key) {
    if (root == nullptr) return root;
    if (key &gt; root-&gt;val) {
      root-&gt;right = deleteNode(root-&gt;right, key);
    } else if (key &lt; root-&gt;val) {
      root-&gt;left = deleteNode(root-&gt;left, key);
    } else {
      TreeNode* new_root = nullptr;
      if (root-&gt;left == nullptr) {
        new_root = root-&gt;right;
      } else if (root-&gt;right == nullptr) {
        new_root = root-&gt;left;
      } else {
        // Find the min node in the right sub tree
        TreeNode* parent = root;
        new_root = root-&gt;right;
        while (new_root-&gt;left != nullptr) {
          parent = new_root;
          new_root = new_root-&gt;left;
        }
        
        if (parent != root) {
          parent-&gt;left = new_root-&gt;right;
          new_root-&gt;right = root-&gt;right;
        }
        
        new_root-&gt;left = root-&gt;left;      
      }
      
      delete root;
      return new_root;
    }
    
    return root;
  }
};</pre><p>v2</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 35 ms
class Solution {
public:
  TreeNode* deleteNode(TreeNode* root, int key) {
    if (root == nullptr) return root;
    if (key &gt; root-&gt;val) {
      root-&gt;right = deleteNode(root-&gt;right, key);
    } else if (key &lt; root-&gt;val) {
      root-&gt;left = deleteNode(root-&gt;left, key);
    } else {
      if (root-&gt;left != nullptr &amp;&amp; root-&gt;right != nullptr) {
        TreeNode* min = root-&gt;right;
        while (min-&gt;left != nullptr) min = min-&gt;left;
        root-&gt;val = min-&gt;val;
        root-&gt;right = deleteNode(root-&gt;right, min-&gt;val);
      } else {
        TreeNode* new_root = root-&gt;left == nullptr ? root-&gt;right : root-&gt;left;
        root-&gt;left = root-&gt;right = nullptr;
        delete root;
        return new_root;
      }
    }    
    return root;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-450-delete-node-in-a-bst/">花花酱 LeetCode 450. Delete Node in a 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-450-delete-node-in-a-bst/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
