当前位置:网站首页>Leetcode 450. 删除二叉搜索树中的节点
Leetcode 450. 删除二叉搜索树中的节点
2022-08-11 03:29:00 【LuZhouShiLi】
Leetcode 450. 删除二叉搜索树中的节点
题目
给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
一般来说,删除节点可分为两个步骤:
首先找到需要删除的节点;
如果找到了,删除它。
思路
- 确定递归函数的参数和返回值:根节点,目标值。
- 确定终止条件:遇到空结点就返回,说明没找到删除的节点,遍历到空结点之后就直接返回了
- 找到了删除的节点:
- 左右孩子都是空 叶子节点 ,直接删除即可
- 被删除节点的左孩子是空,右孩子不为空,直接删除该节点,右孩子作为新的根节点
- 被删除节点的右孩子是空,左孩子不为空,直接删除该节点,左孩子作为新的根节点
- 被删除节点的左右孩子都不为空,将被删除节点的左子树连接到被删除节点的右子树中的最左节点下,被删除节点的右子树顶替根节点
代码
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if(root == nullptr)
{
return nullptr;
}
if(key > root->val)
{
// 当前节点值小于目标值 在右子树中寻找
root->right = deleteNode(root->right,key);
}
else if(key < root->val)
{
// 当前节点值大于 目标值
root->left= deleteNode(root->left,key);
}
else
{
// 找到目标节点值
if(root->left == nullptr)
{
return root->right;
}
if(root->right == nullptr)
{
return root->left;
}
// 删除的节点 左右子树都存在
// 寻找删除节点右子树中的最左节点
TreeNode* node = root->right;
while(node->left != nullptr)
{
node = node->left;
}
// 将被删除节点的左子树连接到 上述寻找到的最左节点
node->left = root->left;
// 被删除的节点的右子顶替为位置 节点被删除
root = root->right;
}
return root;
}
};
边栏推荐
- leetcode: 358. Reorder strings at K distance intervals
- console.log alternatives you didn't know about
- Idea (preferred) cherry-pick operation
- 7 sorting algorithms that are often tested in interviews
- How does MSP430 download programs to the board?(IAR MSPFET CCS)
- En-us is an invalid culture error solution when Docker links sqlserver
- 程序化交易改变了什么?
- 【愚公系列】2022年08月 Go教学课程 035-接口和继承和转换与空接口
- Environment configuration of ESP32 (arduino arduino2.0 VScode platform which is easy to use?)
- App基本框架搭建丨日志管理 - KLog
猜你喜欢
The problem that Merge will be lost again after code Revert has been solved
Redis老了吗?Redis与Dragonfly性能比较
When EasyCVR is connected to the GB28181 device, what is the reason that the device is connected normally but the video cannot be played?
Rotary array problem: how to realize the array "overall reverse, internal orderly"?"Three-step conversion method" wonderful array
互换性测量技术-几何误差
AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis
Briefly, talk about the use of @Transactional in the project
Environment configuration of ESP32 (arduino arduino2.0 VScode platform which is easy to use?)
DOM-DOM tree, a DOM tree has three types of nodes
字体反扒
随机推荐
输入起始位置,终止位置截取链表
[ADI low-power 2k code] Based on ADuCM4050, ADXL363, TMP75 acceleration, temperature detection and serial port printing, buzzer playing music (lone warrior)
Goodbye Guangzhou paper invoices!The issuance of electronic invoices for accommodation fees will completely replace the invoices of hotels, restaurants and gas stations
How to delete statements audit log?
作业8.10 TFTP协议 下载功能
The impact of programmatic trading and subjective trading on the profit curve!
App Basic Framework Construction丨Log Management - KLog
互换性测量与技术——偏差与公差的计算,公差图的绘制,配合与公差等级的选择方法
Detailed explanation of VIT source code
EasyCVR接入海康大华设备选择其它集群服务器时,通道ServerID错误该如何解决?
多串口RS485工业网关BL110
【Yugong Series】August 2022 Go Teaching Course 036-Type Assertion
互换性测量技术-几何误差
What is third-party payment?
[BX]和loop
[yu gong series] Go program 035-08 2022 interfaces and inheritance and transformation and empty interface
“顶梁柱”滑坡、新增长极难担重任,阿里“蹲下”是为了跳更高?
互换性与测量技术——表面粗糙度选取和标注方法
typedef定义结构体数组类型
E-commerce project - mall time-limited seckill function system