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 ...
随机推荐
- Scyther Advanced Topics
建立非对称秘钥对 声明一个公钥函数和一个私钥函数: const pk2: Function ; const sk2: Function : 我们还声明这些函数代表非对称密钥对: inversekeys ...
- 2.Git 结构
1.Git 结构: 使用git add命令将写的代码暂存到暂存区:使用git commit命令将暂存区的代码提交到本地库: 2. Git 结构及其代码托管中心: workSpace:工作区(写代码). ...
- Poj-3286- How many 0's? - 【基础数位DP】
How many 0's? Description A Benedict monk No.16 writes down the decimal representations of all natur ...
- dos中查找端口的PID,并在任务管理器中处理端口
本文来源https://www.cnblogs.com/lsyf/p/8979012.html 1.查看所有端口进程 首先点击开始菜单选择运行,接着在运行对话框中输入“cmd”,回车打开命令提示符窗口 ...
- python开发全自动网站链接主动提交百度工具
自己网站因数据比较多,趁晚上没事就写了一个通过python爬取url自动提交给百度,实现网站全站提交的思路,代码实现很简单,因为编写时间仓储,难免有些bug,可以放在服务器上配置下定时爬取提交. im ...
- js 面向对象之属性描述符
上回介绍了面向对象之构造器属性.这次介绍下属性描述符 遍历对象属性 let person = {name: "lisi"} for (key in person) { consol ...
- 多任务udp聊天器完整版
import socket import threading def send_msg(udp_socket,dest_ip,dest_port): while True: send_data = i ...
- python中的lambda()函数
语句:print map(lambda x:x ** 2,[1,2,3,4,5]) 其中lambda()函数在Python文档,文档中解释如下: lambda An anonymous inline ...
- 密码加密与微服务鉴权JWT详细使用
[TOC] 1.1.了解微服务状态 微服务集群中的每个服务,对外提供的都是Rest风格的接口,而Rest风格的一个最重要的规范就是:服务的无状态性. 什么是无状态? 1.服务端不保存任何客户端请求者信 ...
- centos6.5 安装163yum源
1.下载yum源 http://mirrors.163.com/.help/CentOS6-Base-163.repo 2.把下载好的yum源放到/etc/yum.repos.d下 mv CentOS ...