【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. Return the root node reference (possibly updated) of the BST.
Basically, the deletion can be divided into two stages:
- Search for a node to remove.
- If the node is found, delete the node.
Example 1:Input: root = [5,3,6,2,4,null,7],
key = 3Output: [5,4,6,2,null,null,7]
Explanation: Given key to delete is 3. So we find the node with value 3 and delete it. One valid answer is [5,4,6,2,null,null,7], shown in the above BST. Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
首先对于BST 来说左子树的节点都比根节点小,右子树的节点都比根节点大,由此可以递归寻找到需要删除的节点。
在删除当前节点的时候需要在该节点的子树中寻找替换节点,同时还要保证BST特性,有点像堆的插入和删除,为了保证依旧保持BST特性,那么替换的节点就是左子树的最大值,或者右子树的最小值,按照这个思路可以把代码写出来。
1、复杂版本 想看最优化的代码请看2号答案,但是下面这个答案确确实实是我第一时间想到的写法,deleteNode 用于检索需要删除的节点,find_right和 find_left 分别用于寻找左子树的最大值,或者右子树的最小值,由于需要替换节点所以还保存了前后两个节点用于删除子树,这个写法很麻烦。而且代码没有完全oc,oc了95%,不知道问题在哪儿。
/**
* 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) {
//递归 检索由于是bst 二叉树 根据其特性可以快速查找对应的节点
if(root==nullptr) return nullptr; if(root->val<key){
root->right=deleteNode(root->right,key);
}
else if(root->val>key){
root->left=deleteNode(root->left,key);
}
else if(root->val==key){
// 替换当前节点 需要寻找右子树的最小节点 或者左子树的最大节点
// 如果右子树为空 就得去找左子树 如果左子树为空就得找右子树
TreeNode* node=nullptr;
if(root->right!=nullptr){
node= find_right(root,root->right); //寻找右子树的最小节点
node->left=root->left;
node->right=root->right; }
else if(root->left!=nullptr){
node= find_left(root,root->left); //寻找左子树的最大节点
node->left=root->left;
node->right=root->right;
}
return node;
}
return root;
} TreeNode* find_right(TreeNode* node,TreeNode* node1){
//存储最后两个节点
TreeNode* root=node;
while(node1->left!=nullptr){
node=node1;
node1=node1->left;
}
if(node==root){
node->right=node1->right;
}
else{
node->left=node1->left;
}
return node1; } TreeNode* find_left(TreeNode* node,TreeNode* node1){
//存储最后两个节点
TreeNode* root=node;
while(node1->right!=nullptr){
node=node1;
node1=node1->right; //寻找左子树最大的数
}
if(node==root){
node->left=node1->left;
}
else{
node->right=node1->right;
}
return node1;
}
};
2、优化版本,在替换节点的时候可以分三种情况判断以下,(1)无左右子树、(2)只有左子树或者右子树(只要的话删除当前节点即可)、(3)同时有左子树或者右子树,替换后再调用一次deleteNode 即可
/**
* 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) {
//递归 检索由于是bst 二叉树 根据其特性可以快速查找对应的节点
if(root==nullptr) return nullptr; if(root->val<key){
root->right=deleteNode(root->right,key);
}
else if(root->val>key){
root->left=deleteNode(root->left,key);
}
else if(root->val==key){
// 替换当前节点 需要寻找右子树的最小节点 或者左子树的最大节点
// 分三种情况讨论论
// 1 当前节点没有左右子树
if(!root->left && !root->right) return nullptr;
// 2 当前节点只有左子树 或者右子树
else if(!root->left || !root->right)
return root->left ? root->left:root->right;
// 3 当前节点同时有 左子树和右子树 则用左子树的最大节点替换当前节点
TreeNode* node=root->left;
while(node->right!=nullptr) node=node->right;
// 替换当前节点
root->val=node->val;
//但是还要 删除掉这个最右边的节点 下边这个复杂的答案是同时存储两个节点 手动删除
//但是 仔细思考一下 删除这个最右边的节点不就是再调用一遍deleteNode 函数嘛?
root->left=deleteNode(root->left,node->val); }
return root;
} };
【leetcode】 450. Delete Node in a BST的更多相关文章
- 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...
- 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...
- 【LeetCode】237. Delete Node in a Linked List
题目: Write a function to delete a node (except the tail) in a singly linked list, given only access t ...
- LeetCode OJ 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 ...
- 【leetcode】955. Delete Columns to Make Sorted II
题目如下: We are given an array A of N lowercase letter strings, all of the same length. Now, we may cho ...
- [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 ...
- 【一天一道LeetCode】#237. Delete Node in a Linked List
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- [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 ...
- 【leetcode】1273. Delete Tree Nodes
题目如下: A tree rooted at node 0 is given as follows: The number of nodes is nodes; The value of the i- ...
随机推荐
- 暴力尝试安卓gesture.key
import hashlib import os import itertools f = open(r'D:\KEY\gesture.key','r') psd = f.readline() f.c ...
- shell 脚本控制命令的执行顺序
&&,||,(),{},& 五个符号的运用shell脚本执行命令的时候,有时候会依赖于前一个命令是否执行成功.而&&和||就是用来判断前一个命令执行效果的. 也 ...
- 三、其他主机安装zabbix-agent加入到zabbix
一.yum (rpm)方式 1,下载安装对应的zabbix-agent的rpm包 rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/ ...
- HCNP Routing&Switching之BGP路由过滤和AS-Path-Filter
前文我们聊了下通过修改BGP路由属性来影响路由,从而达到控制BGP路由的目的:回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15495585.html:今天我们 ...
- Python 爬取 煎蛋
这个我是拿来参考的 import requests def url_open(url): response = requests.get(url) html = response.content re ...
- 多云搭建 K3S 集群
作者:SRE运维博客 博客地址: https://www.cnsre.cn/ 文章地址:https://www.cnsre.cn/posts/211119132529/ 相关话题:https://ww ...
- [loj539]旅游路线
考虑枚举加油的位置,当确定某次在第$i$个位置加油后,且下一次到$j$加油,那么$i$到$j$必然会选择不超过$c_{i}$条边且最长的路径,记作$d_{i,j}$ 如果能求出$d_{i,j}$,再设 ...
- [cf643G]Choosing Ads
首先对于$p>50$,有经典的做法,即不断删去区间中不同的两数,最终剩下的即为出现次数超过一半的数(或没有),用线段树维护即可 那么对于$p\le 50$,类似的,即删去区间中不同的$\lflo ...
- SpringCloud微服务实战——搭建企业级开发框架(二十五):实现多租户多平台短信通知服务
目前系统集成短信似乎是必不可少的部分,由于各种云平台都提供了不同的短信通道,这里我们增加多租户多通道的短信验证码,并增加配置项,使系统可以支持多家云平台提供的短信服务.这里以阿里云和腾讯云为例,集成短 ...
- SPOJ 1557 GSS2 - Can you answer these queries II (线段树+维护历史最值)
都说这题是 GSS 系列中最难的,今天做了一下,名副其实 首先你可以想到各种各样的在线乱搞想法,线段树,主席树,平衡树,等等,但发现都不太可行. 注意到题目也没有说强制在线,因此可以想到离线地去解决这 ...