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:

  1. Search for a node to remove.
  2. 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的更多相关文章

  1. [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 ...

  2. 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. ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. 【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 ...

  7. 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...

  8. 450 Delete Node in a BST 删除二叉搜索树中的结点

    详见:https://leetcode.com/problems/delete-node-in-a-bst/description/ C++: /** * Definition for a binar ...

  9. [leetcode]450. Delete Node in a BST二叉搜索树删除节点

    二叉树变量只是一个地址 public static void main(String[] args) { TreeNode t = new TreeNode(3); help(t); System.o ...

随机推荐

  1. 商汤开源的mmdetection技术报告

    目录 1. 简介 2. 支持的算法 3. 框架与架构 6. 相关链接 前言:让我惊艳的几个库: ultralytics的yolov3,在一众yolov3的pytorch版本实现算法中脱颖而出,收到开发 ...

  2. Eclipse安装zookeeper监控插件

    1.在 Eclipse 菜单打开Help -> Install New Software…2.添加 url http://www.massedynamic.org/eclipse/updates ...

  3. zoj 4099 Extended Twin Composite Number

    Do you know the twin prime conjecture? Two primes  and  are called twin primes if . The twin prime c ...

  4. python 'NoneType' object has no attribute 'get'

    获取 页面链接的时候报错 'NoneType' object has no attribute 'get' href = div.find("a").get("href& ...

  5. Java图形界面

    图形界面 JFrame在swingbao JFrame jframe = new JFrame(); iframe.setVisible(true); //设置窗口显示 jframe.setLocat ...

  6. mongodb中帮助信息和命令

    在Mongodb中,可以看作是一种面向对象的操作,如果你对与某一个操作不清楚,可以直接help. 在mongodb中,无非是对DB.user.collections.文档的操作. 下面是简单的示例: ...

  7. 通过n+1个控制点求出n段分段函数的解析式

    最近刚好学了解析几何,在学完二元一次方程组与一次函数的关系后,我突然有了一个大胆的想法! 可不可以用程序自动求出一次函数的解析式呢? 这个想法源自于最近一段时间数学练习册上百考不厌的同类型题:给定在一 ...

  8. oracle存储过程把查询到的值更新到别的表

    create or replace procedure update_nst_t_Clime2 as cursor c_db is select * from NST_T_FRAME f ,) as ...

  9. 使用VS创建三层架构的项目

    使用VS创建三层架构的项目 1.打开VS软件: 2.创建新项目: 3.创建新项目打开后选择SAP的选项点击创建{注:上面还有一个和这个相似的项目创建:看清楚在选择: 4.创建后改下名字,框架如果不是3 ...

  10. start django project

    1.django-admin startproject helloword 创建项目helloword 2.开始一个app,写一个hello worldpython manage.py startap ...