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 ...
随机推荐
- java - day013 - 流, FileInputStream, BufferedInputStream,
流 Stream 把数据的读写,抽象成数据在管道中流动. 流是单向的 输入流, 只能用来读取数据 输出流, 只能用来输出数据 流只能顺序读写数据 流只能一次性从头到尾读写数据 流动过的数据,不能反复流 ...
- awk初级教程
参考:sed & awk 概述 sed & awk指令组成 与sed区别 尽管awk指令与sed指令的结构相同,都由模式和过程两部分组成,但过程本身有很大不同. awk看上去不像编辑器 ...
- 【转】java注解处理器——在编译期修改语法树
https://blog.csdn.net/a_zhenzhen/article/details/86065063 前言从需求说起由于相关政策,最近公司安全部要求各系统在rpc接口调用的交互过程中把相 ...
- Android加载大图到内存如何避免内存溢出?
加载大图怎么避免溢出实际做法就是对图像进行压缩,也是比较老的话题了,在最初做android时是经常会遇到的问题,而如今对于图片加载这一块都已经有很成熟稳定的三方库来弄它了,所以图片加载过大内存溢出的比 ...
- Mybatis的一级缓存机制简介
1.接口 public interface MemberMapperCache { public Members selectMembersById(Integer id); } 2.配置文件xml ...
- numpy 参考:https://mp.weixin.qq.com/s?__biz=MzU1MjYzNjQwOQ==&mid=2247486010&idx=1&sn=e42e6706e0e285ecbfdbbd76fb4ff352&chksm=fbfe50accc89d9ba56a3167c519638f1327a5c5bf12ed59dd8c6de9b2c25baeec1f1f8ad5fb7&
a=np.array([,,,]) b=np.arange() print(a,b) [ ] [ ] 对应相乘 c2=a*b [ 0 2 6 12] 对应相乘再求和 c3=a.dot( ...
- 【CSP模拟赛】God knows (李超线段树)
题面 CODE 稍微分析一下,发现把(i,pi)(i,p_i)(i,pi)看做二维数点,就是求极长上升子序列的权值最小值. 直接李超线段树 #include <bits/stdc++.h> ...
- linux中的set ff=unix
set ff=unix : 告诉 vi 编辑器,使用unix换行符. 操作步骤: 1.用vi命令打开文件 2.直接输入 :set ff=unix
- MAC OS系统替换homebrew使用阿里云的镜像源
MAC OS系统替换homebrew使用阿里云的镜像源 2019-03-03 15:13:42 南通SEO 阅读数 2024更多 分类专栏: 解决方案 MAC OS系统替换homebrew使用阿里 ...
- SpringMVC使用ResponseEntity实现文件下载,及图片base64的字节数组上传于下载
本文主要通过ResponseEntity<byte[]>实现文件下 该类实现响应头.文件数据(以字节存储).状态封装在一起交给浏览器处理以实现浏览器的文件下载. ResponseEntit ...