{"id":2358,"date":"2018-03-24T13:36:48","date_gmt":"2018-03-24T20:36:48","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2358"},"modified":"2018-03-31T08:31:30","modified_gmt":"2018-03-31T15:31:30","slug":"leetcode-450-delete-node-in-a-bst","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-450-delete-node-in-a-bst\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 450. Delete Node in a BST"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 450. Delete Node in a BST - \u5237\u9898\u627e\u5de5\u4f5c EP177\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/00r9qf7lgAk?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><\/p>\n<h1>Problem<\/h1>\n<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>\n<div class=\"question-description\">\n<div>\n<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>\n<p>Basically, the deletion can be divided into two stages:<\/p>\n<ol>\n<li>Search for a node to remove.<\/li>\n<li>If the node is found, delete the node.<\/li>\n<\/ol>\n<p><b>Note:<\/b>\u00a0Time complexity should be O(height of tree).<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"crayon:false \">root = [5,3,6,2,4,null,7]\r\nkey = 3\r\n\r\n    5\r\n   \/ \\\r\n  3   6\r\n \/ \\   \\\r\n2   4   7\r\n\r\nGiven key to delete is 3. So we find the node with value 3 and delete it.\r\n\r\nOne valid answer is [5,4,6,2,null,null,7], shown in the following BST.\r\n\r\n    5\r\n   \/ \\\r\n  4   6\r\n \/     \\\r\n2       7\r\n\r\nAnother valid answer is [5,2,6,null,4,null,7].\r\n\r\n    5\r\n   \/ \\\r\n  2   6\r\n   \\   \\\r\n    4   7<\/pre>\n<\/div>\n<\/div>\n<h1><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/h1>\n<h1><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/h1>\n<h1><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/h1>\n<h1><strong>Solution: Recursion<\/strong><\/h1>\n<p>Time complexity: O(h)<\/p>\n<p>Space complexity: O(h)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 36 ms\r\n\/**\r\n * Definition for a binary tree node.\r\n * struct TreeNode {\r\n *     int val;\r\n *     TreeNode *left;\r\n *     TreeNode *right;\r\n *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}\r\n * };\r\n *\/\r\nclass Solution {\r\npublic:\r\n  TreeNode* deleteNode(TreeNode* root, int key) {\r\n    if (root == nullptr) return root;\r\n    if (key &gt; root-&gt;val) {\r\n      root-&gt;right = deleteNode(root-&gt;right, key);\r\n    } else if (key &lt; root-&gt;val) {\r\n      root-&gt;left = deleteNode(root-&gt;left, key);\r\n    } else {\r\n      TreeNode* new_root = nullptr;\r\n      if (root-&gt;left == nullptr) {\r\n        new_root = root-&gt;right;\r\n      } else if (root-&gt;right == nullptr) {\r\n        new_root = root-&gt;left;\r\n      } else {\r\n        \/\/ Find the min node in the right sub tree\r\n        TreeNode* parent = root;\r\n        new_root = root-&gt;right;\r\n        while (new_root-&gt;left != nullptr) {\r\n          parent = new_root;\r\n          new_root = new_root-&gt;left;\r\n        }\r\n        \r\n        if (parent != root) {\r\n          parent-&gt;left = new_root-&gt;right;\r\n          new_root-&gt;right = root-&gt;right;\r\n        }\r\n        \r\n        new_root-&gt;left = root-&gt;left;      \r\n      }\r\n      \r\n      delete root;\r\n      return new_root;\r\n    }\r\n    \r\n    return root;\r\n  }\r\n};<\/pre>\n<p>v2<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 35 ms\r\nclass Solution {\r\npublic:\r\n  TreeNode* deleteNode(TreeNode* root, int key) {\r\n    if (root == nullptr) return root;\r\n    if (key &gt; root-&gt;val) {\r\n      root-&gt;right = deleteNode(root-&gt;right, key);\r\n    } else if (key &lt; root-&gt;val) {\r\n      root-&gt;left = deleteNode(root-&gt;left, key);\r\n    } else {\r\n      if (root-&gt;left != nullptr &amp;&amp; root-&gt;right != nullptr) {\r\n        TreeNode* min = root-&gt;right;\r\n        while (min-&gt;left != nullptr) min = min-&gt;left;\r\n        root-&gt;val = min-&gt;val;\r\n        root-&gt;right = deleteNode(root-&gt;right, min-&gt;val);\r\n      } else {\r\n        TreeNode* new_root = root-&gt;left == nullptr ? root-&gt;right : root-&gt;left;\r\n        root-&gt;left = root-&gt;right = nullptr;\r\n        delete root;\r\n        return new_root;\r\n      }\r\n    }    \r\n    return root;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<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>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[74,274,28],"class_list":["post-2358","post","type-post","status-publish","format-standard","hentry","category-tree","tag-bst","tag-delete","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2358","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=2358"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2358\/revisions"}],"predecessor-version":[{"id":2404,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2358\/revisions\/2404"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}