[LeetCode] 450. Delete Node in a BST
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.
Example 1:
Input: root = [5,3,6,2,4,null,7], key = 3 Output: [5,4,6,2,null,null,7] Explanation: 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.
Example 2:
Input: root = [5,3,6,2,4,null,7], key = 0 Output: [5,3,6,2,4,null,7] Explanation: The tree does not contain a node with value = 0.
Example 3:
Input: root = [], key = 0 Output: []
Constraints:
- The number of nodes in the tree is in the range
[0, 104]
. -105 <= Node.val <= 105
- Each node has a unique value.
root
is a valid binary search tree.-105 <= key <= 105
Follow up: Could you solve it with time complexity O(height of tree)
?
删除二叉搜索树中的节点。
给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
一般来说,删除节点可分为两个步骤:
首先找到需要删除的节点;
如果找到了,删除它。来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/delete-node-in-a-bst
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题意是给一个二叉搜索树和一个节点 key,请将 key 从 BST 中删除并维持 BST 的性质。
因为是 BST 所以查找要删除的节点应该很简单,如果比 root 小就往左走,比 root 大就往右走。麻烦的地方在于删除了节点之后如何维持剩下的节点依然是一个 BST。分如下几种情况,
1. 如果要删除的节点没有左子树,则直接用这个节点的右孩子顶上来
2. 如果要删除的节点没有右子树,则直接用这个节点的左孩子顶上来
3. 如果要删除的节点有左右子树,需要去右子树里面找出最小的节点顶上来,这里我选择的是右子树里面最小的左孩子。其实也可以选左孩子里面最大的右孩子
对于第一种情况来说:我们要删除节点5(root),直接 return root.right 即可。
对于第二种情况来说:我们要删除节点5(root),直接 return root.left 即可。
对于第三种情况来说:我们要删除节点5(root),只需将root的左子树放到root的右子树的最下面的左叶子节点的左子树上即可。如图所示:
作者:ming-zhi-shan-you--m9RfkvKDad
链接:https://leetcode-cn.com/problems/delete-node-in-a-bst/solution/450-shan-chu-er-cha-sou-suo-shu-zhong-de-jie-dia-6/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
时间O(h) - 题目要求
空间O(h) - 题目要求
Java实现
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 class Solution { 11 public TreeNode deleteNode(TreeNode root, int key) { 12 // corner case 13 if (root == null) { 14 return root; 15 } 16 17 // normal case 18 if (key < root.val) { 19 root.left = deleteNode(root.left, key); 20 } else if (key > root.val) { 21 root.right = deleteNode(root.right, key); 22 } else { 23 if (root.left == null) { 24 return root.right; 25 } else if (root.right == null) { 26 return root.left; 27 } else { 28 TreeNode node = root.right; 29 while (node.left != null) { 30 node = node.left; 31 } 32 node.left = root.left; 33 return root.right; 34 } 35 } 36 return root; 37 } 38 }
JavaScript实现
1 /** 2 * @param {TreeNode} root 3 * @param {number} key 4 * @return {TreeNode} 5 */ 6 var deleteNode = function (root, key) { 7 // corner case 8 if (root == null) { 9 return null; 10 } 11 12 // normal case 13 if (key < root.val) { 14 root.left = deleteNode(root.left, key); 15 } else if (key > root.val) { 16 root.right = deleteNode(root.right, key); 17 } else { 18 if (root.left == null) { 19 return root.right; 20 } else if (root.right == null) { 21 return root.left; 22 } 23 let minNode = findMin(root.right); 24 root.val = minNode.val; 25 root.right = deleteNode(root.right, root.val); 26 } 27 return root; 28 }; 29 30 var findMin = function (node) { 31 while (node.left !== null) { 32 node = node.left; 33 } 34 return node; 35 };