UNPKG

ds-algo-study

Version:

Just experimenting with publishing a package

53 lines (50 loc) 2.64 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="description" content="A description of the page and its contents" /> <link rel="stylesheet" href="styles.css" /> <title>Page Title</title> <link rel="stylesheet" href="./../../../assets/style.css" /> <link rel="stylesheet" href="./../../../assets/prism.css" /> <script async src="./../../../assets/prism.js"></script> </head> <body> <h1 id="delete-node-in-a-bst">Delete Node in a BST</h1> <blockquote> <p>Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.</p> </blockquote> <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 type="1"> <li>Search for a node to remove.</li> <li>If the node is found, delete the node.</li> </ol> <p><strong>Follow up:</strong> Can you solve it with time complexity <code>O(height of tree)</code>?</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" /></p> <p><strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3 <strong>Output:</strong> [5,4,6,2,null,null,7] <strong>Explanation:</strong> 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 above BST. Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted. <img src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" /> </p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0 <strong>Output:</strong> [5,3,6,2,4,null,7] <strong>Explanation:</strong> The tree does not contain a node with value = 0.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> root = [], key = 0 <strong>Output:</strong> []</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 104]</code>.</li> <li><code>-105 &lt;= Node.val &lt;= 105</code></li> <li>Each node has a <strong>unique</strong> value.</li> <li><code>root</code> is a valid binary search tree.</li> <li><code>-105 &lt;= key &lt;= 105</code></li> </ul> <p><a class="btn" href="https://leetcode.com/problems/delete-node-in-a-bst/">Source</a></p> </body> </html>