Leetcode450. 删除二叉搜索树中的节点
思路:
(1)如果root为空,返回
(2)如果当前结点root是待删除结点:
a:root是叶子结点,直接删去即可
b:root左子树不为空,则找到左子树的最大值,即前驱结点,使用前驱结点代替待删除的root结点值,并在root的左子树中,继续删除前驱结点
c:root右子树不为空,则找到右子树的最大值,即后继结点,使用后继结点代替待删除的root结点值,并在root的右子树中,继续删除后继结点
(3)如果待删除值大于root->val,则在右子树中继续删除待删除值
(4)如果待删除值小于root->val,则在左子树中继续删除待删除值
void deleteTreeNode(TreeNode* &root,int key)//使用引用,以可以直接改变原树的结点
{
if(root==NULL) return;
if(root->val==key)
{
if(!root->left && !root->right)
{
root=NULL;
}
else if(root->left)//左子树不空就优先左子树吧
{
//找左子树的最大元素,,即找到前驱结点
TreeNode* cur=root->left;
while(cur->right)
cur=cur->right;
root->val=cur->val;//使用前驱结点覆盖目标结点,并在目标结点的左子树中删除前驱结点
deleteTreeNode(root->left,cur->val);
}
else if(root->right)//右子树不空就优先左子树吧
{
//找右子树的最大元素,,即找到后继结点
TreeNode* cur=root->right;
while(cur->left)
cur=cur->left;
root->val=cur->val;//使用后继结点覆盖目标结点,并在目标结点的右子树中删除后继结点
deleteTreeNode(root->right,cur->val);
}
}
else if(key < root->val)
{
deleteTreeNode(root->left,key);
}
else if(key > root->val)
{
deleteTreeNode(root->right,key);
}
}
要注意,上面递归的过程中使用的是root的引用,因为是要直接删除结点,而不是改变结点中的值。
Leetcode450. 删除二叉搜索树中的节点的更多相关文章
- [Swift]LeetCode450. 删除二叉搜索树中的节点 | Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- Java实现 LeetCode 450 删除二叉搜索树中的节点
450. 删除二叉搜索树中的节点 给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变.返回二叉搜索树(有可能被更新)的根节点的引 ...
- [LeetCode] Delete Node in a BST 删除二叉搜索树中的节点
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- [LeetCode] 450. Delete Node in a BST 删除二叉搜索树中的节点
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- 给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用
一般来说,删除节点可分为两个步骤: 首先找到需要删除的节点: 如果找到了,删除它. 说明: 要求算法时间复杂度为 O(h),h 为树的高度. 示例: root = [5,3,6,2,4,null,7] ...
- 450 Delete Node in a BST 删除二叉搜索树中的结点
详见:https://leetcode.com/problems/delete-node-in-a-bst/description/ C++: /** * Definition for a binar ...
- LeetCode 230. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)
230. 二叉搜索树中第K小的元素 230. Kth Smallest Element in a BST 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的 ...
- 刷题-力扣-230. 二叉搜索树中第K小的元素
230. 二叉搜索树中第K小的元素 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/kth-smallest-element-in-a ...
- [Swift]LeetCode701. 二叉搜索树中的插入操作 | Insert into a Binary Search Tree
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...
随机推荐
- springboot 打包jar 运行找资源文件
使用如下方式读取文件:ClassPathResource cpr = new ClassPathResource("static/ok.png");byte[] bdata = F ...
- 一些webpack常见编译报错的解决方案
重新安装依赖可以解决80%的webpack编译报错问题. rm -rf node_modules rm package-lock.json npm cache clear --force npm in ...
- 入职小白随笔之Android四大组件——广播详解(broadcast)
Broadcast 广播机制简介 Android中的广播主要可以分为两种类型:标准广播和有序广播. 标准广播:是一种完全异步执行的广播,在广播发出之后,所有的广播接收器几乎都会在同一时刻接收到这条广播 ...
- RMAN异机恢复主要步骤和注意事项
以后改行了或老了回头看看,我曾经会这些,也是件愉快的事 [备份]--创建目录[oracle@test20 backup]$ mkdir -p /home/oracle/backup--备份脚本[ora ...
- Database mirroring connection error 4 'An error occurred while receiving data: '10054(An existing connection was forcibly closed by the remote host.)
公司一SQL Server镜像发生了故障转移(主备切换),检查SQL Server镜像发生主备切换的原因,在错误日志中发现下面错误: Date 2019/8/31 14:09:17 ...
- 11.2 Data Guard Physical Standby Switchover Best Practices using SQL*Plus (Doc ID 1304939.1)
11.2 Data Guard Physical Standby Switchover Best Practices using SQL*Plus (Doc ID 1304939.1) APPLIES ...
- September 15th, 2019. Sunday, Week 38th.
Break down these walls and come on in. 一路披荆斩棘,勇往直前. We are the only wall that stands in our way to s ...
- 05-Node.js学习笔记-第三方模块
5.1什么是第三方模块 别人写好的,具有特定功能的,我们能直接使用的模块即第三方模块,由于第三方模块通常都是由多个文件组成并且被放置在一个文件夹中,所以又名包. 第三方模块有两种存在形式 以js文件的 ...
- Python-类的几种调用方法
一:实例 二:静态 可以调用类以外的变量,只限于此模块. 三:类方法 可以调用该类中定义的变量进行使用. 直接上代码
- 《IM开发新手入门一篇就够:从零开发移动端IM》
登录 立即注册 TCP/IP详解 资讯 动态 社区 技术精选 首页 即时通讯网›专项技术区›IM开发新手入门一篇就够:从零开发移动端IM 帖子 打赏 分享 发表评论162 想开 ...