LeetCode - Medium - 450

Basically, the deletion can be divided into two stages:

  1. Search for a node to remove.

  2. If the node is found, delete the node.

Follow up: Can you solve it with time complexity O(height of tree)?

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 , 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


方法一:我写的。

  1. 找出目标节点,找到即返回它和它的父节点,找不到直接结束算法。

  2. 如果目标节点是根节点,就给它弄个临时父节点,原根节点成为它的左子树。

  3. 根据目标节点的左右子树的有无指定删除方案:

  • 目标节点是叶子节点,或只有一个左子树,或只有一个右子树,则采用类似单链表删除节点方法(这就需要用到目标节点的父节点)。

  • 目标节点都有左右子树,则从右子树中查找出最小值节点,让它值替换目标节点值,随后在右子树移除最小值节点。由于BST的性质,最小值节点有么是叶子节点,有么只有右子树,只有这两种情况,则放心采用类似单链表删除节点方法移除最小值节点。

  1. 最后,返回根节点。如果目标节点是根节点,则返回临时父节点的左子树。

方法二:别人写的,递归版,精简优雅。

  1. Recursively find the node that has the same value as the key, while setting the left/right nodes equal to the returned subtree.
  1. 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;

}

//方法二:别人写的,递归版,精简优雅了许多

public TreeNode deleteNode2(TreeNode root, int key) {

if(root == null){

return null;

}

if(key < root.val){

root.left = deleteNode2(root.left, key);

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
]

[外链图片转存中…(img-VurpwS2a-1714936936511)]

[外链图片转存中…(img-rqyjzjl5-1714936936511)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 26
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值