【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- ...
随机推荐
- JAVA笔记14__多线程共享数据(同步)/ 线程死锁 / 生产者与消费者应用案例 / 线程池
/** * 多线程共享数据 * 线程同步:多个线程在同一个时间段只能有一个线程执行其指定代码,其他线程要等待此线程完成之后才可以继续执行. * 多线程共享数据的安全问题,使用同步解决. * 线程同步两 ...
- 【mysql1】如何删除MySQL内存|不再跟新系列
完全卸载mysql的具体步骤: 包括停止服务 + 卸载相关程序 + 删除注册表等等 步骤一: windows键+R-->Control-->程序和功能:所有MySQL程序点击右键 ...
- typedef的用法 单向链表的查找、增加、删除、销毁。
一:typedef的用法. 写一个数据结构(计算机存储数据的一种方式,是抽象的,可以人为组织,提高算法效率),我们需要注意:接口友好,模块化,规范命名等方面,在接口友好方面,typedef是非常 ...
- CTF-Tools 一款CTF古典密码加解密工具
CTF-Tools 一款CTF古典密码加解密工具 工具截图 工具简介 一款CTF编码.解码.加密.解密工具. 支持的编码解码: URL-UTF-8 URL-GB2312 Unicode Escape( ...
- 学习JS的第三天
一.逻辑分支(续) 1.三目运算符:条件运算符 a>b?c:d;表达式1?表达式2:表达式3; 根据表达式1执行的结果,来决定执行表达式2还是表达式3 表达式1结果是true执行表达式2,最终返 ...
- elasitcsearch单机版安装
1.下载压缩包 https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.2.tar.gz 2.解压修改配置文件 c ...
- CAP 5.2 版本发布通告
前言 今天,我们很高兴宣布 CAP 发布 5.2 版本正式版,在这个版本中,我们主要致力于更好的优化使用体验以及支持新的 Transport,同时在该版本也进行了一些 bug 修复的工作. 自从 5. ...
- java miniui(datagrid) 取值赋值
... (本想放项目代码上来的 可是这放上去就看不了不知为何,下面是项目中的代码) // 获取时间设置到时间上,加载没有那个快,所以需要先加载完数据然后才能获取 function obtain(e) ...
- VS Code Java 更新 – 全新Gradle for Java插件,更方便的代码操作, 1.0 语言支持发布
大家好,欢迎来到 9 月版的 Visual Studio Code Java 更新.在这篇文章中,我们将分享我们最新的Gradle插件,更加方便的代码操作(Getter/Setter等等),以及最近的 ...
- vue.js学习与实战笔记(2)
驼峰式写法时需要注意的问题 学习到组件这一章时,由于没注意到vue中对于camelCased的解释,导致出错了都找不出来,后面发现 在使用驼峰式写法时,在使用模板的时候需要使用kebab-case命名 ...