{"id":109,"date":"2019-06-02T13:51:32","date_gmt":"2019-06-02T13:51:32","guid":{"rendered":"https:\/\/www.369usa.com\/blog\/?p=109"},"modified":"2019-06-02T13:51:32","modified_gmt":"2019-06-02T13:51:32","slug":"tech-interview-exam-question-delete-node-in-a-bst-hard-core","status":"publish","type":"post","link":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-delete-node-in-a-bst-hard-core\/","title":{"rendered":"Tech Interview Exam Question: Delete Node in a BST (Hard Core!)"},"content":{"rendered":"<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<p>Search for a node to remove.<br \/>\nIf the node is found, delete the node.<br \/>\nNote: Time complexity should be O(height of tree).<\/p>\n<p>Example:<\/p>\n<p>root = [5,3,6,2,4,null,7]<br \/>\nkey = 3<\/p>\n<p>    5<br \/>\n   \/ \\<br \/>\n  3   6<br \/>\n \/ \\   \\<br \/>\n2   4   7<\/p>\n<p>Given key to delete is 3. So we find the node with value 3 and delete it.<\/p>\n<p>One valid answer is [5,4,6,2,null,null,7], shown in the following BST.<\/p>\n<p>    5<br \/>\n   \/ \\<br \/>\n  4   6<br \/>\n \/     \\<br \/>\n2       7<\/p>\n<p>Another valid answer is [5,2,6,null,4,null,7].<\/p>\n<p>    5<br \/>\n   \/ \\<br \/>\n  2   6<br \/>\n   \\   \\<br \/>\n    4   7<br \/>\nAccepted<br \/>\n66,103<br \/>\nSubmissions<br \/>\n165,217<\/p>\n<pre>\r\n\r\n\/**\r\n * Definition for a binary tree node.\r\n * function TreeNode(val) {\r\n *     this.val = val;\r\n *     this.left = this.right = null;\r\n * }\r\n *\/\r\n\/**\r\n * @param {TreeNode} root\r\n * @param {number} key\r\n * @return {TreeNode}\r\n *\/\r\nvar deleteNode = function(root, key) {\r\n\r\n    var node = root;\r\n    var nodeParent = root;\r\n    var childReference = null;\r\n    \r\n    \/\/Find the node with key\r\n    while(node !== null){\r\n        if(node.val === key){\r\n            break;\r\n        }    \r\n        else if(node.val &lt; key){\r\n            nodeParent = node;\r\n            node = node.right;\r\n            childReference = 'right';\r\n        }\r\n        else{\r\n            nodeParent = node;\r\n            node = node.left;\r\n            childReference = 'left';\r\n        }\r\n    }    \r\n    \r\n    \/\/If node is not in the tree\r\n    if(node === null) return root;\r\n    \r\n    \/\/If node is a leaf\r\n    if(node.left === null &amp;&amp; node.right === null){\r\n        if(childReference === null) return null;\/\/Root node matches the key\r\n        nodeParent[childReference] = null;      \r\n    }\r\n    \/\/If node is has one child\r\n    else if(node.left === null){\r\n        if(childReference === null) return node.right;\/\/Root node matches the key\r\n        nodeParent[childReference] = node.right;\r\n         \r\n    }\r\n    else if(node.right === null){\r\n        if(childReference === null) return node.left;\/\/Root node matches the key\r\n        nodeParent[childReference] = node.left;\r\n    }\r\n    \/\/If node is has both children\r\n    else{\r\n        var value = findMinNode(node.right);  \r\n        node.val = value;\r\n        node.right = deleteNode(node.right,value);\r\n    }\r\n\r\n    return root;\r\n    \r\n};\r\n\r\nvar findMinNode = function(node){\r\n    if(node === null) return false;\r\n    while(node.left !== null){\r\n        node = node.left;\r\n    }\r\n    return node.val;\r\n}\r\n\r\n<\/pre>\n<p><b>I spent the whole morning on this and this is the result I got from the leetcode:<\/b><\/p>\n<p>Runtime: 92 ms, faster than 86.76% of JavaScript online submissions for Delete Node in a BST.<\/p>\n<p>Memory Usage: 42.2 MB, less than 32.28% of JavaScript online submissions for Delete Node in a BST.<\/p>\n","protected":false},"excerpt":{"rendered":"<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. Basically, the deletion can be divided into two stages: Search for a node to remove. If the node is found, delete the node. Note: &hellip; <a href=\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-delete-node-in-a-bst-hard-core\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Tech Interview Exam Question: Delete Node in a BST (Hard Core!)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-109","post","type-post","status-publish","format-standard","hentry","category-6"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Tech Interview Exam Question: Delete Node in a BST (Hard Core!) - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-delete-node-in-a-bst-hard-core\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tech Interview Exam Question: Delete Node in a BST (Hard Core!) - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\" \/>\n<meta property=\"og:description\" content=\"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. Basically, the deletion can be divided into two stages: Search for a node to remove. If the node is found, delete the node. Note: &hellip; Continue reading &quot;Tech Interview Exam Question: Delete Node in a BST (Hard Core!)&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-delete-node-in-a-bst-hard-core\/\" \/>\n<meta property=\"og:site_name\" content=\"\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\" \/>\n<meta property=\"article:published_time\" content=\"2019-06-02T13:51:32+00:00\" \/>\n<meta name=\"author\" content=\"darkersss\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"darkersss\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-delete-node-in-a-bst-hard-core\/\",\"url\":\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-delete-node-in-a-bst-hard-core\/\",\"name\":\"Tech Interview Exam Question: Delete Node in a BST (Hard Core!) - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\",\"isPartOf\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/#website\"},\"datePublished\":\"2019-06-02T13:51:32+00:00\",\"dateModified\":\"2019-06-02T13:51:32+00:00\",\"author\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-delete-node-in-a-bst-hard-core\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-delete-node-in-a-bst-hard-core\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-delete-node-in-a-bst-hard-core\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.369usa.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tech Interview Exam Question: Delete Node in a BST (Hard Core!)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#website\",\"url\":\"https:\/\/www.369usa.com\/blog\/\",\"name\":\"369\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.369usa.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6\",\"name\":\"darkersss\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g\",\"caption\":\"darkersss\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Tech Interview Exam Question: Delete Node in a BST (Hard Core!) - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-delete-node-in-a-bst-hard-core\/","og_locale":"en_US","og_type":"article","og_title":"Tech Interview Exam Question: Delete Node in a BST (Hard Core!) - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","og_description":"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. Basically, the deletion can be divided into two stages: Search for a node to remove. If the node is found, delete the node. Note: &hellip; Continue reading \"Tech Interview Exam Question: Delete Node in a BST (Hard Core!)\"","og_url":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-delete-node-in-a-bst-hard-core\/","og_site_name":"\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","article_published_time":"2019-06-02T13:51:32+00:00","author":"darkersss","twitter_card":"summary_large_image","twitter_misc":{"Written by":"darkersss","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-delete-node-in-a-bst-hard-core\/","url":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-delete-node-in-a-bst-hard-core\/","name":"Tech Interview Exam Question: Delete Node in a BST (Hard Core!) - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","isPartOf":{"@id":"https:\/\/www.369usa.com\/blog\/#website"},"datePublished":"2019-06-02T13:51:32+00:00","dateModified":"2019-06-02T13:51:32+00:00","author":{"@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6"},"breadcrumb":{"@id":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-delete-node-in-a-bst-hard-core\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-delete-node-in-a-bst-hard-core\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-delete-node-in-a-bst-hard-core\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.369usa.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Tech Interview Exam Question: Delete Node in a BST (Hard Core!)"}]},{"@type":"WebSite","@id":"https:\/\/www.369usa.com\/blog\/#website","url":"https:\/\/www.369usa.com\/blog\/","name":"369\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.369usa.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6","name":"darkersss","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g","caption":"darkersss"}}]}},"_links":{"self":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/109"}],"collection":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":1,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":110,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/109\/revisions\/110"}],"wp:attachment":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}