当前位置:网站首页>Leetcode 450. 删除二叉搜索树中的节点 [二叉搜索树]
Leetcode 450. 删除二叉搜索树中的节点 [二叉搜索树]
2022-06-13 02:17:00 【JackDawn!?】
题目

思路
如果要删除节点的值大于当前根节点,则向右子树递归删除;如果要删除节点的值小于当前根节点,则向左子树递归删除;如果当前节点就是要删除的节点,则需要考虑当前根节点的值被哪个值所替换:
- 如果右子树不为空,则将右子树中最小的值赋给当前根节点;
- 如果右子树为空,但左子树不为空,则将左子树中最大的值赋给当前节点;
- 如果左右子树均为空,则将当前节点置为空即可
代码
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if(root == nullptr) return root;
else if(key > root->val) {
root->right = deleteNode(root->right, key);
} else if(key < root->val) {
root->left = deleteNode(root->left, key);
} else {
if(root->right) {
if(root->right->left) {
auto t = root->right;
auto pre = t;
while(t->left) {
pre = t;
t = t->left;
}
root->val = t->val;
pre->left = t->right;
delete t;
} else {
root->val = root->right->val;
auto t = root->right;
root->right = root->right->right;
delete t;
}
} else if(root->left) {
if(root->left->right) {
auto t = root->left;
auto pre = t;
while(t->right) {
pre = t;
t = t->right;
}
root->val = t->val;
pre->right = t->left;
delete t;
} else {
root->val = root->left->val;
auto t = root->left;
root->left = root->left->left;
delete t;
}
}
else {
return nullptr;
}
}
return root;
}
};
执行结果

边栏推荐
- Basic exercise of test questions decimal to hexadecimal
- Gome's ambition of "folding up" app
- Think about the possibility of attacking secure memory through mmu/tlb/cache
- (novice to) detailed tutorial on machine / in-depth learning with colab from scratch
- Mac使用Docker安装Oracle
- STM32 timer interrupt learning notes
- Jump model between mirrors
- Huawei equipment is configured with CE dual attribution
- Armv8-m (Cortex-M) TrustZone summary and introduction
- Ruixing coffee 2022, extricating itself from difficulties and ushering in a smooth path
猜你喜欢

LabVIEW large project development tools to improve quality
![[pytorch]fixmatch code explanation (super detailed)](/img/22/66703bea0f8ee40eceb0687fcb3ad2.jpg)
[pytorch]fixmatch code explanation (super detailed)

Armv8-m (Cortex-M) TrustZone summary and introduction

Stm32 mpu6050 servo pan tilt support follow

In addition to the full screen without holes under the screen, the Red Devils 7 series also has these black technologies

The commercial value of Kwai is being seen by more and more brands and businesses

Ctrip reshapes new Ctrip

Ten thousand words make it clear that synchronized and reentrantlock implement locks in concurrency

Classification and summary of system registers in aarch64 architecture of armv8/arnv9

华为设备配置虚拟专用网FRR
随机推荐
柏瑞凯电子冲刺科创板:拟募资3.6亿 汪斌华夫妇为大股东
Yovo3 and yovo3 tiny structure diagram
Record: how to solve the problem of "the system cannot find the specified path" in the picture message uploaded by transferto() of multipartfile class [valid through personal test]
1000 fans ~
C language complex type description
Swiper horizontal rotation grid
[work with notes] MFC solves the problem that pressing ESC and enter will automatically exit
STM32F103 IIC OLED program migration complete engineering code
LeetCode每日一题——890. 查找和替换模式
Application circuit and understanding of BAT54C as power supply protection
Basic exercise of test questions decimal to hexadecimal
What did Hello travel do right for 500million users in five years?
How to solve the problem of obtaining the time through new date() and writing out the difference of 8 hours between the database and the current time [valid through personal test]
How to do Internet for small enterprises
js获取元素
Restrict cell input type and display format in CXGRID control
分享三个关于CMDB的小故事
Paper reading - group normalization
Application and routine of C language typedef struct
Huawei equipment configures private IP routing FRR