Example 1:
Input: root = [5,3,6,2,4,null 《大厂前端面试题解析+Web核心总结学习笔记+企业项目实战源码+最新高清讲解视频》无偿开源 徽信搜索公众号【编程进阶路】 ,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 , 1 0 4 ] [0, 10^4] [0,104].
-
− 1 0 5 < = N o d e . v a l < = 1 0 5 -10^5 <= Node.val <= 10^5 −105<=Node.val<=105
-
Each node has a unique value.
-
root
is a valid binary search tree. -
− 1 0 5 < = k e y < = 1 0 5 -10^5 <= key <= 10^5 −105<=key<=105
[](()Analysis
方法一:我写的。
-
找出目标节点,找到即返回它和它的父节点,找不到直接结束算法。
-
如果目标节点是根节点,就给它弄个临时父节点,原根节点成为它的左子树。
-
根据目标节点的左右子树的有无指定删除方案:
-
目标节点是叶子节点,或只有一个左子树,或只有一个右子树,则采用类似单链表删除节点方法(这就需要用到目标节点的父节点)。
-
目标节点都有左右子树,则从右子树中查找出最小值节点,让它值替换目标节点值,随后在右子树移除最小值节点。由于BST的性质,最小值节点有么是叶子节点,有么只有右子树,只有这两种情况,则放心采用类似单链表删除节点方法移除最小值节点。
- 最后,返回根节点。如果目标节点是根节点,则返回临时父节点的左子树。
方法二:别人写的,递归版,精简优雅。
- Recursively find the node that has the same value as the key, while setting the left/right nodes equal to the returned subtree.
- Once the node is found, have to handle the below 4 cases:
* node doesn’t have left or right - return null
* node only has left subtree- return the left subtree
* node only has right subtree- return the right subtree
* node has both left and right - find the minimum value in the right subtree, set that value to the currently found node, then
recursively delete the minimum value in the right subtree
[link](()
[](()Submission
import com.lun.util.BinaryTree.TreeNode;
public class DeleteNodeInABST {
//方法一:我写的
public TreeNode deleteNode(TreeNode root, int key) {
TreeNode[] result = find(root, key);
if(result != null) {
TreeNode parent = result[0], target = result[1];
TreeNode fakeRoot = null;
if(parent == null) {//删除根节点,弄个假根节点,原
fakeRoot = new TreeNode(Integer.MAX_VALUE, root, null);
}
if(target.left != null && target.right != null) {//既有左子树,有右子树
TreeNode replaceOne = findReplaceOneFromRight(target, target.right);
target.val = replaceOne.val;
}else {
removeEasyOne(parent == null ? fakeRoot: parent, target);
}
if(parent == null) {//删除根节点的情况
root = fakeRoot.left;
fakeRoot.left = null;
}
}
return root;
}
private TreeNode[] find(TreeNode root, int key) {
TreeNode last = null, curr = root;
while(curr != null) {
if(key < curr.val) {
last = curr;
curr = curr.left;
}else if(curr.val < key){
last = curr;
curr = curr.right;
}else {
return new TreeNode[] {last, curr};
}
}
return null;
}
/**
-
target是叶子节点,target只有左子树,target只有右子数,这三种情况可以用链表式方法的删除节点
*/
private void removeEasyOne(TreeNode parent, TreeNode target) {
if(target.left == null && target.right == null) {//target是叶子节点
if(parent.left == target){
parent.left = null;
}else {
parent.right = null;
}
}else if(target.left != null && target.right == null){//target只有左子树
if(parent.left == target){
parent.left = target.left;
target.left = null;
}else {
parent.right = target.left;
target.left = null;
}
}else if(target.left == null && target.right != null){//target只有右子数
if(parent.left == target){
parent.left = target.right;
target.right = null;
}else {
parent.right = target.right;
target.right = null;
}
}
}
//查找右子树的最小值
private TreeNode findReplaceOneFromRight(TreeNode last, TreeNode child) {
if(child == null) return null;
while(child.left != null) {//
last = child;
child = child.left;
}
removeEasyOne(last, child);
return child;
}