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

边栏推荐
- Area of basic exercise circle ※
- When AI meets music, iFLYTEK music leads the industry reform with technology
- Review the history of various versions of ITIL, and find the key points for the development of enterprise operation and maintenance
- 反爬虫策略(ip代理、设置随机休眠时间、哔哩哔哩视频信息爬取、真实URL的获取、特殊字符的处理、时间戳的处理、多线程处理)
- [unity] problems encountered in packaging webgl project and their solutions
- C language complex type description
- Barrykay electronics rushes to the scientific innovation board: it is planned to raise 360million yuan. Mr. and Mrs. Wang Binhua are the major shareholders
- Leetcode daily question - 890 Find and replace mode
- The execution results of i+=2 and i++ i++ under synchronized are different
- [51nod.3210] binary Statistics (bit operation)
猜你喜欢

Why is Huawei matebook x Pro 2022 leading a "laptop" revolution
![[pytorch]fixmatch code explanation - data loading](/img/0f/1165dbe4c7410a72d74123ec52dc28.jpg)
[pytorch]fixmatch code explanation - data loading
![[unity] problems encountered in packaging webgl project and their solutions](/img/f4/13696831b27becc2e45a712bb79f4c.png)
[unity] problems encountered in packaging webgl project and their solutions

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

Share three stories about CMDB

Top level configuration + cooling black technology + cool appearance, the Red Devils 6S Pro is worthy of the flagship game of the year

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

0- blog notes guide directory (all)

When AI meets music, iFLYTEK music leads the industry reform with technology

SQLserver2008 拒绝了对对象 '****' (数据库 '****',架构 'dbo')的 SELECT 权限
随机推荐
Introduction to armv8/armv9 - learning this article is enough
Gadgets: color based video and image cutting
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]
分享三个关于CMDB的小故事
[work with notes] NDK compiles the open source library ffmpeg
Detailed explanation of C language conditional compilation
(novice to) detailed tutorial on machine / in-depth learning with colab from scratch
SQL Server 删除数据库所有表和所有存储过程
Huawei equipment configures private IP routing FRR
QT realizes mind mapping function (II)
Gome's ambition of "folding up" app
柏瑞凱電子沖刺科創板:擬募資3.6億 汪斌華夫婦為大股東
Sqlserver2008 denied select permission on object'***** '(database'*****', schema'dbo')
Stm32+ze-08 formaldehyde sensor tutorial
STM32 steering gear controller
Application and routine of C language typedef struct
Understand HMM
Swiper horizontal rotation grid
ROS learning-6 detailed explanation of publisher programming syntax
Classification and summary of system registers in aarch64 architecture of armv8/arnv9