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:

  1. Search for a node to remove.
  2. 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的更多相关文章

  1. 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...

  2. 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...

  3. 【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 ...

  4. 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 ...

  5. 【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 ...

  6. [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 ...

  7. 【一天一道LeetCode】#237. Delete Node in a Linked List

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

  8. [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 ...

  9. 【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- ...

随机推荐

  1. 第08课 OpenGL 混合

    混合: 在这一课里,我们在纹理的基础上加上了混合,它看起具有透明的效果,当然解释它不是那么容易,当希望你喜欢它. 简单的透明OpenGL中的绝大多数特效都与某些类型的(色彩)混合有关.混色的定义为,将 ...

  2. coreseek使用心得

    基本使用方法: D:\coreseek-4.1\bin\searchd -c D:\coreseek-4.1\etc\article.conf --stop 停止服务 D:\coreseek-4.1\ ...

  3. ansible基本命令及剧本

    ansible常用命令 1. -v, –verbose 详细模式,如果命令执行成功,输出详细的结果(-vv –vvv -vvvv) 2. -i, –inventory=PATH 指定host文件的路径 ...

  4. SpringBoot2.x异步任务EnableAsync

    1.springboot启动类里面使用@EnableAsync注解开启异步功能 @EnableAsync public class Demo001Application { public static ...

  5. 怎么将本地已有的一个项目上传到新建的git仓库的方法

    将本地已有的一个非git项目上传到新建的git仓库的方法一共有两种. 一. 克隆+拷贝 第一种方法比较简单,直接用把远程仓库拉到本地,然后再把自己本地的项目拷贝到仓库中去.然后push到远程仓库上去即 ...

  6. JAVA POI导出EXCEL 动态表头、多级表头、动态数据

    导出Excel文件是业务中经常遇到的需求,以下是经常遇到的一些问题: 1,导出中文文件名乱码 String filename = "sheet1";response.setChar ...

  7. 大一C语言学习笔记(3)---对于程序员来说,学历和能力,到底哪个重要?

    在高考失利后,我合理地萎靡一段时间,振作起来之后选择了我憧憬了10年的计算机专业---软件工程.但由于分数受限,也是选择了二本普通院校黑科技(我当然爱她,我的母校),而因为学历上的自卑,让我有了想考研 ...

  8. Python爬虫中的URLError\HTTPError异常类,异常的抛出

    # _*_ coding : utf-8 _*_# @Time : 2021/11/2 14:20# @Author : 秋泊酱 import urllib.request import urllib ...

  9. ABP Framework 5.0 RC.1 新特性和变更说明

    .Net 6.0 发布之后,ABP Framework 也在第一时间进行了升级,并在一个多星期后(2021-11-16)发布了 5.0 RC.1 ,新功能和重要变更基本已经确定. 5.0版本新特性 新 ...

  10. Rabbitmq的死信

    一.概述 死信有死信队列.死信交换器和死信消息组成.死信消息则有如下三种情况生成: 1.消费者使用basic.reject或 basic.nack并将requeue参数设置为false来拒绝该消息 2 ...