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:

  1. root = [5,3,6,2,4,null,7]
  2. key = 3
  3.  
  4. 5
  5. / \
  6. 3 6
  7. / \ \
  8. 2 4 7
  9.  
  10. Given key to delete is 3. So we find the node with value 3 and delete it.
  11.  
  12. One valid answer is [5,4,6,2,null,null,7], shown in the following BST.
  13.  
  14. 5
  15. / \
  16. 4 6
  17. / \
  18. 2 7
  19.  
  20. Another valid answer is [5,2,6,null,4,null,7].
  21.  
  22. 5
  23. / \
  24. 2 6
  25. \ \
  26. 4 7
  1. class Solution {
  2. public:
  3. TreeNode* deleteNode(TreeNode* root, int key) {
  4. if(!root) return root;
  5. TreeNode* ret;
  6. if(root->val == key) {
  7. TreeNode* rnode_lmost = getlm_or_rm_node(root->right, true);
  8. if(rnode_lmost) {
  9. rnode_lmost->left = root->left;
  10. ret = root->right;
  11. }else ret = root->left;
  12. }else {
  13. if(key < root->val) root->left = deleteNode(root->left, key);
  14. else root->right = deleteNode(root->right, key);
  15. ret = root;
  16. }
  17. return ret;
  18. }
  19. TreeNode* getlm_or_rm_node(TreeNode* root, bool left){
  20. if(!root) return root;
  21. if(left) {
  22. while(root->left) root = root->left;
  23. }else {
  24. while(root->right) root = root->right;
  25. }
  26. return root;
  27. }
  28. };
  1. class Solution {
  2. public:
  3. TreeNode *deleteNode(TreeNode *root, int key) {
  4. TreeNode **cur = &root;
  5.  
  6. while (*cur && (*cur)->val != key)
  7. cur = (key > (*cur)->val) ? &(*cur)->right : &(*cur)->left;
  8.  
  9. if (*cur) {
  10. if (!(*cur)->right) *cur = (*cur)->left;
  11. else {
  12. TreeNode **successor = &(*cur)->right;
  13. while ((*successor)->left) successor = &(*successor)->left;
  14. swap((*cur)->val, (*successor)->val);
  15. *successor = (*successor)->right ? (*successor)->right : nullptr;
  16. }
  17. }
  18. return root;
  19. }
  20.  
  21. };

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. Windows下动态库的制作与使用

    创建静态库

  2. charles 主界面总结

    本文参考:charles 主界面总结 charles 主界面的介绍 Charles 主要提供两种查看封包的视图,分别名为 Structure Structure/结构视图,将网络请求按访问的域名分类, ...

  3. Luogu P1892 团伙

    Luogu P1892 团伙 这是道很简单的并查集-- 不,它并不简单. 这道题考了一个叫做反集的东西. 也就是说: 如果$a$和$b$是敌人,合并$n+b$和$a$,$n+a$和$b$: 如果$c$ ...

  4. 详细讲解vue.js里的父子组件通信(props和$emit)

    在进入这个话题之前,首先我们先来想一下在vue里,如何写一个父子组件.为了简单起见,下面的代码我都没用脚手架来构建项目,直接在html文件里引入vue.js来作为例子.父子组件的写法如下: <d ...

  5. vueRouter history模式下 nginx配置

    对于VUE的router[mode: history]模式(这里主要是为了去除链接上的"#")开发的时候,一般都不出问题.是因为开发时用的服务器为node,Dev环境中已配置好了, ...

  6. DML子句returing into用法举例

    一.概述: ORACLE的DML语句中可以指定RETURNING语句.使用起来也很简单,和SELECT INTO语句没有多大区别.RETURNING语句的使用在很多情况下可以简化PL/SQL编程. I ...

  7. Scrapy框架的八个扩展

    一.proxies代理 首先需要在环境变量中设置 from scrapy.contrib.downloadermiddleware.httpproxy import HttpProxyMiddlewa ...

  8. LightOJ-1020-A Childhood Game(博弈)

    链接: https://vjudge.net/problem/LightOJ-1020 题意: Alice and Bob are playing a game with marbles; you m ...

  9. docker-compose.yml的使用

    docker-compose.yml包含version.services.networks3大部分 services的书写规则 1.iamge services: web: # 服务名称,用户自定义 ...

  10. java实现大视频上传

    javaweb上传文件 上传文件的jsp中的部分 上传文件同样可以使用form表单向后端发请求,也可以使用 ajax向后端发请求 1.通过form表单向后端发送请求 <form id=" ...