LC 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.
Note: Time complexity should be O(height of tree).
Example:
root = [5,3,6,2,4,null,7]
key = 3 5
/ \
3 6
/ \ \
2 4 7 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 following BST. 5
/ \
4 6
/ \
2 7 Another valid answer is [5,2,6,null,4,null,7]. 5
/ \
2 6
\ \
4 7
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if(!root) return root;
TreeNode* ret;
if(root->val == key) {
TreeNode* rnode_lmost = getlm_or_rm_node(root->right, true);
if(rnode_lmost) {
rnode_lmost->left = root->left;
ret = root->right;
}else ret = root->left;
}else {
if(key < root->val) root->left = deleteNode(root->left, key);
else root->right = deleteNode(root->right, key);
ret = root;
}
return ret;
}
TreeNode* getlm_or_rm_node(TreeNode* root, bool left){
if(!root) return root;
if(left) {
while(root->left) root = root->left;
}else {
while(root->right) root = root->right;
}
return root;
}
};
class Solution {
public:
TreeNode *deleteNode(TreeNode *root, int key) {
TreeNode **cur = &root; while (*cur && (*cur)->val != key)
cur = (key > (*cur)->val) ? &(*cur)->right : &(*cur)->left; if (*cur) {
if (!(*cur)->right) *cur = (*cur)->left;
else {
TreeNode **successor = &(*cur)->right;
while ((*successor)->left) successor = &(*successor)->left;
swap((*cur)->val, (*successor)->val);
*successor = (*successor)->right ? (*successor)->right : nullptr;
}
}
return root;
} };
LC 450. Delete Node in a BST的更多相关文章
- [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 ...
- 450. Delete Node in a BST 删除bst中的一个节点
[抄题]: Given a root node reference of a BST and a key, delete the node with the given key in the BST. ...
- [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 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 ...
- 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】 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】450. Delete Node in a BST 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...
- 450 Delete Node in a BST 删除二叉搜索树中的结点
详见:https://leetcode.com/problems/delete-node-in-a-bst/description/ C++: /** * Definition for a binar ...
- [leetcode]450. Delete Node in a BST二叉搜索树删除节点
二叉树变量只是一个地址 public static void main(String[] args) { TreeNode t = new TreeNode(3); help(t); System.o ...
随机推荐
- Linux proc filesystem (procfs)
参考:/proc /proc简介 本文着重关注/proc目录,查看其中文件并熟悉它. /proc目录存在于所有Linux系统上,无论什么发行版或体系结构.首先,必须澄清一个误解: 就文件系统这一术语而 ...
- python字典添加元素和删除元素
1. 添加字典元素 方法一:直接添加,给定键值对 #pycharm aa = {'人才':60,'英语':'english','adress':'here'} print(aa) # {'人才': 6 ...
- SOUL软件小结
soul 基于心灵的智能社交APP.功能是寻找最适合自己的灵魂伴侣 基于心灵测试给你智能匹配最简单的社交关系 匿名聊天软件一般都是没有机器人的,机器人一般不能对点聊很长时间 用户来源与动机 用户引流来 ...
- Socket的一些疑惑整理
关于listen的问题请看steven<tcp/ip详解1>18章18.11.4 呼入连接请求队列一节,说的很清楚
- 【搜索-剪枝-偏难】PAT-天梯赛-L3-015. 球队“食物链”
L3-015. 球队“食物链” 某国的足球联赛中有N支参赛球队,编号从1至N.联赛采用主客场双循环赛制,参赛球队两两之间在双方主场各赛一场. 联赛战罢,结果已经尘埃落定.此时,联赛主席突发奇想,希望从 ...
- Robot Framework--变量
Robot Framework的变量分为标量, 列表和字典, 分别使用语法格式 ${SCALAR}, @{LIST} 和 &{DICT} 来定义. 此外, 环境变量可以直接使用语法 %{ENV ...
- SQL SERVER 语句转换格式函数Cast、Convert
CAST.CONVERT都可以执行数据类型转换.在大部分情况下,两者执行同样的功能,不同的是CONVERT还提供一些特别的日期格式转换,而CAST没有这个功能. CAST是ANSI兼容的,推荐使用CO ...
- H3C常见视图及命令
H3C常见视图及命令 H3C Comware的视图模式 1.用户视图:查看系统的硬件和系统的信息 2.系统视图(类似于Cisco的配置模式) 3.路由协议视图 4.接口视图 5.用户界面视图 各种视图 ...
- java中创建对象的方式
Java中有5种创建对象的方式,下面给出它们的例子还有它们的字节码 使用new关键字 } → 调用了构造函数 使用Class类的newInstance方法 } → 调用了构造函数 使用Construc ...
- CentOS环境部署(Nginx+Mariadb+Java+Tomcat)
1.安装nginx 安装 yum install nginx 启动 yum install nginx 开机自启 sudo systemctl enable nginx 2.安装mariadb 安装 ...