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 ...
随机推荐
- Flutter——Checkbox组件、CheckboxListTile(多选框组件)
Checkbox组件 Checkbox组件常用的属性: 属性 描述 value true 或者 false onChanged 改变的时候触发的事件 activeColor 选中的颜色.背景颜色 c ...
- Linux命令——procinfo
简介 proc文件系统是一个虚拟文件系统,包含有关进程和系统信息的文件. proc 文件系统开机时自动挂载并映射到/proc目录.许多程序从/proc目录中检索信息,对其进行处理并使其易于用于各种目的 ...
- You are what you say!
说话的方式表现了你的角色,所以说话的口吻在不同场合要贴合自己的身份和角色 尖锐的问题:如果要说一个比较尖锐的问题,那么可以把这个问题推理一下,从比较生活化的一点开始,或者将问题推给虚拟的第三方(但是注 ...
- SpringCloud之Eureka
[前面的话]SpringCloud为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它配置简单,上手快,而且生态成 ...
- HTTP 状态码(常见及分析)
首先得明白状态码的几个大类: 状态码 响应类别 出现原因 1XX 信息性状态码(Informational) 服务器正在处理请求 2XX 成功状态码(Success) 请求已正常处理完毕 3XX 重定 ...
- 题解 洛谷P1290 【欧几里德的游戏】
这题没必要那么麻烦,只需要推理一下即可: 假设我们有两个数\(x,y\),先把\(x\)设为较大值,\(y\)设为较小值.现在分成三种情况: \(1\).若两数为倍数关系,操作的一方赢. \(2\). ...
- 性能三 powerVR specfication
2.Optimising Geometry Interleaving Attributes VBO Draw call size Triangle Size 32个像素/primitive - ...
- WebForm FindControl的使用方法
Control.FindControl (String):在当前的命名容器中搜索带指定 id参数的服务器控件. 有点类似javascript中的getElementById(string); 简单的例 ...
- 你所不知道的printf函数
#include <stdio.h> int main(void) { int a = 4; int b = 3; int c = a / b; float d = *(float *)( ...
- Codeforces Round #413 (Div1 + Div. 2) C. Fountains(树状数组维护最大值)
题目链接:https://codeforces.com/problemset/problem/799/C 题意:有 c 块硬币和 d 块钻石,每种喷泉消耗硬币或钻石中的一种,每个喷泉有一个美丽值,问建 ...