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);
}else if(key > root.val){
root.right = deleteNode2(root.right, key);
}else{
if(root.left == null){
return root.right;
}else if(root.right == null){
return root.left;
}
TreeNode minNode = findMin(root.right);
root.val = minNode.val;
root.right = deleteNode2(root.right, root.val);
}
return root;
}
private TreeNode findMin(TreeNode node){
while(node.left != null){
node = node.left;
}
return node;
}
}
import static org.junit.Assert.*;
import org.junit.Test;
import com.lun.util.BinaryTree;
import com.lun.util.BinaryTree.TreeNode;
public class DeleteNodeInABSTTest {
@Test
public void test() {
DeleteNodeInABST obj = new DeleteNodeInABST();
TreeNode root1 = obj.deleteNode(BinaryTree.integers2BinaryTree(5,3,6,2,4,null,7), 3);
TreeNode expected1 = BinaryTree.integers2BinaryTree(5,4,6,2,null,null,7);
assertTrue(BinaryTree.equals(root1, expected1));
TreeNode root2 = obj.deleteNode(BinaryTree.integers2BinaryTree(5,3,6,2,4,null,7), 0);
TreeNode expected2 = BinaryTree.integers2BinaryTree(5,3,6,2,4,null,7);
assertTrue(BinaryTree.equals(root2, expected2));
assertNull(obj.deleteNode(null, 0));
//删除根节点
TreeNode root3 = obj.deleteNode(BinaryTree.integers2BinaryTree(5,3,6,2,4,null,7), 5);
TreeNode expected3 = BinaryTree.integers2BinaryTree(6,3,7,2,4);
assertTrue(BinaryTree.equals(root3, expected3));
assertNull(obj.deleteNode(new TreeNode(0), 0));
}
@Test
public void test2() {
DeleteNodeInABST obj = new DeleteNodeInABST();
TreeNode root1 = obj.deleteNode2(BinaryTree.integers2BinaryTree(5,3,6,2,4,null,7), 3);
TreeNode expected1 = BinaryTree.integers2BinaryTree(5,4,6,2,null,null,7);
assertTrue(BinaryTree.equals(root1, expected1));
TreeNode root2 = obj.deleteNode2(BinaryTree.integers2BinaryTree(5,3,6,2,4,null,7), 0);
TreeNode expected2 = BinaryTree.integers2BinaryTree(5,3,6,2,4,null,7);
assertTrue(BinaryTree.equals(root2, expected2));
assertNull(obj.deleteNode2(null, 0));
//删除根节点
TreeNode root3 = obj.deleteNode2(BinaryTree.integers2BinaryTree(5,3,6,2,4,null,7), 5);
TreeNode expected3 = BinaryTree.integers2BinaryTree(6,3,7,2,4);
assertTrue(BinaryTree.equals(root3, expected3));
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)
最后
给大家分享一些关于HTML的面试题,有需要的朋友可以戳这里免费领取,先到先得哦。
[外链图片转存中…(img-ALXMUm3Y-1713545405515)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)
[外链图片转存中…(img-7SLoFQRA-1713545405515)]
最后
给大家分享一些关于HTML的面试题,有需要的朋友可以戳这里免费领取,先到先得哦。
[外链图片转存中…(img-YWWjJn8w-1713545405515)]
[外链图片转存中…(img-aoANhibE-1713545405516)]