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
  27.  
  28. 思路:分两种情况,一种是要删除的节点只有一个孩子,另一种是要删除的节点有两个孩子;
    如果只有一个孩子,那么我们让这个节点引用到他非空的孩子上去,即root = root.right
    root = root.left;如果有两个孩子的话,那么有两种办法,一种是找他右边的最小值,另一种
    是找他左边的的最大值。将值赋给该节点,然后删除右边最小值或左边最大值;
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public TreeNode deleteNode(TreeNode root, int key) {
  12. if (root==null)
  13. return null;
  14. else if (key<root.val)
  15. root.left = deleteNode(root.left,key);
  16. else if (key>root.val)
  17. root.right = deleteNode(root.right,key);
  18. else {
  19. if (root.left!=null&&root.right!=null){
  20. TreeNode tmp = findMin(root.right);
  21. root.val = tmp.val;
  22. root.right = deleteNode(root.right,root.val);
  23. }
  24. else {
  25. TreeNode tmp = root;
  26. if (root.left==null)
  27. root = root.right;
  28. else if (root.right==null)
  29. root = root.left;
  30. tmp = null;
  31. }
  32. }
  33. return root;
  34. }
  35. private TreeNode findMin(TreeNode root){
  36. if (root==null)
  37. return null;
  38. if (root.left==null)
  39. return root;
  40. return findMin(root.left);
  41. }
  42. }

[Leetcode]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. [leetcode]450. Delete Node in a BST二叉搜索树删除节点

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

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

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

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

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

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

  8. 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. Retur ...

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

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

随机推荐

  1. Java 读书笔记 (十七) Java 重写(Override)与重载(Overload)

    重写(Override) 重写是子类对父类的允许访问的方法的实现过程重新编写,返回值和形参都不能改变,即外壳不变,核心重写. // 如果重写不是相当于重新定义了一个方法?那为什么不直接写,还要exte ...

  2. 浅谈cookie,sessionStorage和localStorage

    cookie:cookie在浏览器和服务器间来回传递 cookie数据不能超过4k 同时每次http请求都会携带cookie,所以cookie只适合保存很小的数据,比如会话标识 cookie只在设置的 ...

  3. GIT的使用流程

    GIT的使用流程 1 github注册流程 1 进入github官网:https://github.com/ 2 注册一个自己的github账号 3 右上角选择New repository 4 进入c ...

  4. Django中用户权限模块

    Django中用户权限模块 1 auth模块 auth模块是Django提供的标准权限管理系统,可以提供用户身份认证, 用户组和权限管理. auth可以和admin模块配合使用, 快速建立网站的管理系 ...

  5. 【爆料】-《布莱顿大学毕业证书》Brighton一模一样原件

    布莱顿大学毕业证[微/Q:2544033233◆WeChat:CC6669834]UC毕业证书/联系人Alice[查看点击百度快照查看][留信网学历认证&博士&硕士&海归&am ...

  6. VMware workstation创建虚拟机console图文

    1. 概述2. 配置入口3. 新建虚拟机向导3.1 类型配置3.2 硬件兼容性3.3 操作系统安装3.4 客户机操作系统类型3.5 客户机的名称位置4. 客户机硬件配置选择4.1 客户机处理器配置4. ...

  7. 微信jssdk config:invalid signature 签名错误 ,问题排查过程

    invalid signature签名错误.建议按如下顺序检查: 确认签名算法正确,可用 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisi ...

  8. 隐马尔可夫模型(HMM)总结

    摘要: 1.算法概述 2.算法推导 3.算法特性及优缺点 4.注意事项(算法过程,调参等注意事项) 5.实现和具体例子 6.适用场合 内容: 1.算法概述 隐马尔科夫模型(Hidden Markov ...

  9. 系统的讲解 - PHP 浮点数高精度运算

    目录 概述 浮点数运算的"锅" 任意精度数学函数 常用数值处理方案 扩展 小结 概述 记录下,工作中遇到的坑 ... 关于 PHP 浮点数运算,特别是金融行业.电子商务订单管理.数 ...

  10. linux安装nvm node版本管理器 nvm常用命令 部署node服务器环境

    1,nvm git地址点击打开链接,安装命令 curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh ...