{"id":5402,"date":"2019-08-08T23:55:02","date_gmt":"2019-08-09T06:55:02","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5402"},"modified":"2019-08-10T10:47:47","modified_gmt":"2019-08-10T17:47:47","slug":"leetcode-1110-delete-nodes-and-return-forest","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-1110-delete-nodes-and-return-forest\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1110. Delete Nodes And Return Forest"},"content":{"rendered":"\n<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\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1110. Delete Nodes And Return Forest - \u5237\u9898\u627e\u5de5\u4f5c EP262\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/SEW3Vofoj_k?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given the&nbsp;<code>root<\/code>&nbsp;of a binary tree, each node in the tree has a distinct value.<\/p>\n\n\n\n<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>\n\n\n\n<p>Return the roots of the trees in the remaining forest.&nbsp; You may return the result in any order.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/07\/01\/screen-shot-2019-07-01-at-53836-pm.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [1,2,3,4,5,6,7], to_delete = [3,5]\n<strong>Output:<\/strong> [[1,2,null,4],[6],[7]]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><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>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion \/ Post-order traversal <\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<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>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(|d| + h)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {\n    vector<TreeNode*> ans;\n    unordered_set<int> s{begin(to_delete), end(to_delete)};\n    function<TreeNode*(TreeNode*)> del = [&](TreeNode* n) -> TreeNode* {\n      if (!n) return nullptr;\n      if (n->left) n->left = del(n->left);\n      if (n->right) n->right = del(n->right);\n      if (!s.count(n->val)) return n;\n      if (n->left) ans.push_back(n->left);\n      if (n->right) ans.push_back(n->right);      \n      return nullptr;\n    };\n    TreeNode* r = del(root);\n    if (r) ans.push_back(r);\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def delNodes(self, root: TreeNode, to_delete: List[int]) -> List[TreeNode]:\n    ans = []\n    ds = set(to_delete)\n    def process(n):\n      if not n: return None\n      n.left, n.right = process(n.left), process(n.right)\n      if n.val not in ds: return n\n      if n.left: ans.append(n.left)\n      if n.right: ans.append(n.right)\n      return None\n    root = process(root)\n    if root: ans.append(root)\n    return ans\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given the&nbsp;root&nbsp;of a binary tree, each node in the tree has a distinct value. After deleting&nbsp;all nodes with a value in&nbsp;to_delete, we are left with&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[274,177,17,28],"class_list":["post-5402","post","type-post","status-publish","format-standard","hentry","category-tree","tag-delete","tag-medium","tag-recursion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5402","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5402"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5402\/revisions"}],"predecessor-version":[{"id":5409,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5402\/revisions\/5409"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}