首页 > 数据库 >[Oracle] LeetCode 450 Delete Node in a BST

[Oracle] LeetCode 450 Delete Node in a BST

时间:2022-09-27 03:33:05浏览次数:71  
标签:Node right TreeNode val 450 BST key root left

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.

Solution

运用递归的方法来进行删除。首先根据 \(key\) 与当前节点的值,来判断是在左子树还是右子树。

当找到节点时,我们在右子树上不断找左子树的最后一个点,将这个点作为节点来补到原来的位置(因为这样才能满足 \(BST\)),然后在右子树上同样地递归来删除那个点

点击查看代码
/**
 * 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) {
        if(!root)return root;
        if(root->val > key){
            root->left = deleteNode(root->left, key);
        }
        else if(root->val < key){
            root->right = deleteNode(root->right, key);
        }
        else{
            if(!root->right)return root->left;
            else if(!root->left)return root->right;
            else{
                TreeNode* tmp = root->right;
                while(tmp->left)tmp=tmp->left;
                root->val = tmp->val;
                root->right = deleteNode(root->right,tmp->val);
            }
        }
        return root;
    }
};

标签:Node,right,TreeNode,val,450,BST,key,root,left
From: https://www.cnblogs.com/xinyu04/p/16733160.html

相关文章

  • node_modules 文件夹下 .bin 隐藏文件夹的作用
    如下图所示:答案:Thatisafolderwherebinaries(executables)fromyournodemodulesarelocated.nodemodules可执行文件的存储文件夹所在。本地安装(默认):将东西......
  • elasticsearch:NoNodeAvailableException[None of the configured nodes are available
    NoNodeAvailableException[Noneoftheconfigurednodesareavailable:[{#transport#-1}{9L7K2EanQGSxD5aAmbzAIw}{localhost}{127.0.0.1:9200}]]这里显示无可用节点......
  • 抽象工厂模式 Abstract Factory
    “对象创建”模式通过“对象创建”模式绕开new,来避免对象创建(new)过程中所导致的紧耦合(依赖具体类),从而支持对象创建的稳定。它是接口抽象之后的第一步工作。典型模式......
  • [Oracle] LeetCode 76 Minimum Window Substring 双指针
    Giventwostringssandtoflengthsmandnrespectively,returntheminimumwindowsubstringofssuchthateverycharacterint(includingduplicates)isin......
  • K8s 网络插件 Calico 报错:Number of node(s) with BGP peering established = 0
    问题现象calico对应的Pod启动失败,报错:Numberofnode(s)withBGPpeeringestablished=0问题分析Calico提供了IP自动检测的方法,默认是使用第一个有效网卡上......
  • 前端Node.js-Day39
    Session认证的局限性:Session认证机制需要配合Cookie才能实现。由于Cookie默认不支持跨域访问,所以,当涉及到前端跨域请求后端接口的时候,需要做很多额外的配置,才能实......
  • NodeJS的安装
    前言虽然这些东西很基本也很简单,但是过段时间就会遗忘,有空记录下吧,反正也不耗费多少时间,后期至少比百度快点。安装步骤Linux下的安装下载安装包下载地址:http://nodejs......
  • centos 安装 nodejs
    二进制安装1.下载解压1wgethttps://cdn.npm.taobao.org/dist/node/v12.16.2/node-v12.16.2-linux-x64.tar.xz2tar-xfnode-v12.16.2-linux-x64.tar.xz3mvnode-......
  • centos 安装 nodejs
    二进制安装1.下载解压wgethttps://cdn.npm.taobao.org/dist/node/v12.16.2/node-v12.16.2-linux-x64.tar.xztar-xfnode-v12.16.2-linux-x64.tar.xzmvnode-v12.16.2-li......
  • mac上webstorm打开不
    当你下载最新版的webstorm,然后替换完旧版本的时候,发现打不开,或者是破解后webstorm打不开的情况时,先卸载webstorm软件。然后在终端里执行以下代码,删除webstorm的残留文件r......