首页 > 编程语言 >Remove Node in Binary Search Tree

Remove Node in Binary Search Tree

时间:2022-12-01 19:05:49浏览次数:30  
标签:Node Binary Search right node val parent TreeNode left


Given a root of Binary Search Tree with unique value for each node. Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still a binary search tree after removal.

设找到的需要删除的节点为node
- 如果node是根节点,删除根节点;
- 如果node的右子树为空,直接将node的左子树赋给node的parent节点;
- 如果node的右子树不为空,则需要找一个node的后继(即在右子树中找一个值最小的节点)替换node;
- 如果找不到node则返回root;

技巧:
因为要删除的节点可能是根节点,因此为了算法的通用性,可以首先new一个dummy节点,该节点的左节点指向根节点,这样处理起来更为方便。

/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root: The root of the binary search tree.
* @param value: Remove the node with given value.
* @return: The root of the binary search tree after removal.
*/
TreeNode* removeNode(TreeNode* root, int value) {
// write your code here
if(!root){
return root;
}

TreeNode* dummy =new TreeNode(0);
dummy->left=root;

TreeNode* parent=findNode(dummy,root,value);

TreeNode* node;
if(parent->left && parent->left->val==value){
node=parent->left;
}else if(parent->right && parent->right->val==value){
node=parent->right;
}else{
return dummy->left;//没有找到的情况
}

deleteNode(parent,node);

return dummy->left;
}

TreeNode* findNode(TreeNode* parent,TreeNode* node,int val){

if(node==NULL){
return parent;
}

if(node->val==val){
return parent;
}

if(node->val<val){
return findNode(node,node->right,val);
}else{
return findNode(node,node->left,val);
}
}

void deleteNode(TreeNode* parent,TreeNode* node){

if(node->right==NULL){
if(parent->left==node){
parent->left=node->left;
}else{
parent->right=node->left;
}
}else{
TreeNode* father=node;
TreeNode* minNode=node->right;

while(minNode->left){
father=minNode;
minNode=minNode->left;
}

//这里容易出错
if(father->left==minNode){
father->left=minNode->right;
}else{
father->right=minNode->right;
}


if(parent->left==node){
parent->left=minNode;
}else{
parent->right=minNode;
}

minNode->left=node->left;
minNode->right=node->right;

}

}

};


标签:Node,Binary,Search,right,node,val,parent,TreeNode,left
From: https://blog.51cto.com/u_15899184/5903602

相关文章

  • lintcode:Binary Tree Serialization
    Designanalgorithmandwritecodetoserializeanddeserializeabinarytree.Writingthetreetoafileiscalled‘serialization’andreadingbackfromthe......
  • ElasticSearch笔记
    原文地址:https://www.kuangstudy.com/bbs/1354069127022583809笔记记录B站狂神说Java的ElasticSearch课程:https://www.bilibili.com/video/BV17a4y1x7zq在学习E......
  • nvm 安装配置 nodejs版本和pnpm 安装和设置
    nvm 安装,下载exe  https://github.com/coreybutler/nvm-windows/releases安装建议设置安装路径其他:  配置nodejs的安装路径: 完成安装之后,可以通过window+R,......
  • Elasticsearch Mapping字段未支持索引导致搜索失效问题处理
    问题描述:生产上Es根据一个时间字段搜索,却没有返回数据问题分析:根据命令:GETindexName/_mapping查看#GETindexName/_mapping{ "indexName":{ "mappin......
  • ElasticSearch面试题
    1.为什么要使用ElasticSearch系统中的数据,随着业务的发展,时间的推移,将会非常多,而业务中往往采用模糊查询进行数据的搜索,而模糊查询会导致查询引擎放弃索引,导致系统......
  • Linux搭建ElasticSearch集群
    前言这是整个ElasticSearch搭建的最后一篇文章,其实对我而言ElasticSearch在Linux上搭建集群写这篇文章意义并不大,只是为了补充这个空白而已,所以这篇文章并不会讲解很详细......
  • ElasticSearch集群数据读写流程
    前言本章作为ElasticSearch分布式集群的附属章节,主要讲解ElasticSearch集群环境下数据是如何读写的,既然讲到读写,那么ElasticSearch的更新就是基于二者的结合,顺带也讲一下......
  • ELasticSearch优化
    硬件优化Elasticsearch的基础是Lucene,所有的素引和文档数据是存储在本地的磁盘中,具体的路径可在ES的配置文件./config/elasticsearch.yml中配置,如下:磁盘在现代服务......
  • ElasticSearch分布式集群
    前言关于ElasticSearch集群概念这里就不多废话了,详细可见ElasticSearch基本介绍、ElasticSearch集群系统架构单节点集群我们可以创建一个索引,为这个索引创建三个分片......
  • SpringBoot整合ElasticSearch-SpringData
    前言之前写过一篇SpringBoot整合ElasticSearch是使用的elasticsearch-rest-high-level-client,这篇文章使用Spring-Data来操作ElasticSearch。关于ElasticSearch的搭建我......