当前位置:网站首页>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;
}
};
执行结果

边栏推荐
- Chapter7-12_ Controllable Chatbot
- ROS learning -5 how function packs with the same name work (workspace coverage)
- Build MySQL environment under mac
- json,xml,txt
- 【Unity】打包WebGL项目遇到的问题及解决记录
- 【 unity】 Problems Encountered in Packaging webgl Project and their resolution Records
- [learning notes] xr872 GUI littlevgl 8.0 migration (file system)
- Area of basic exercise circle ※
- 分享三个关于CMDB的小故事
- 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]
猜你喜欢

传感器:SHT30温湿度传感器检测环境温湿度实验(底部附代码)

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

Mbedtls migration experience

Why is "iFLYTEK Super Brain 2030 plan" more worthy of expectation than "pure" virtual human

Share three stories about CMDB

Configuring virtual private network FRR for Huawei equipment

LabVIEW large project development tools to improve quality

柏瑞凯电子冲刺科创板:拟募资3.6亿 汪斌华夫妇为大股东

STM32 sensorless brushless motor drive

【 unity】 Problems Encountered in Packaging webgl Project and their resolution Records
随机推荐
传感器:SHT30温湿度传感器检测环境温湿度实验(底部附代码)
华为设备配置双反射器优化虚拟专用网骨干层
4.11 introduction to firmware image package
Basic exercises of test questions Fibonacci series
JS get element
Paper reading - group normalization
Laptop touch pad operation
1、 Set up Django automation platform (realize one click SQL execution)
Share three stories about CMDB
Luzhengyao, who has entered the prefabricated vegetable track, still needs to stop being impatient
Deep learning the principle of armv8/armv9 cache
Restrict cell input type and display format in CXGRID control
[the fourth day of actual combat of stm32f401ret6 smart lock project in 10 days] voice control is realized by externally interrupted keys
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]
记录:如何解决MultipartFile类的transferTo()上传图片报“系统找不到指定的路径“问题【亲测有效】
ROS learning-8 pit for custom action programming
Installing Oracle with docker for Mac
Decoding iFLYTEK open platform 2.0 is a fertile land for developers and a source of industrial innovation
柏瑞凯电子冲刺科创板:拟募资3.6亿 汪斌华夫妇为大股东
Is space time attention all you need for video understanding?