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

思路:

注意是BST 已经排好序了。

找到要删除的node;

node 不含左右节点  返回null;

node 只含有左子树,返回左子树

node只含有右子树,返回右子树

node 左右子树都有,找到右子树种最下的值,赋给node,递归地删掉 有字数中最小的元素

 class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if(root==null) return null ;
if(root.val>key) root.left=deleteNode(root.left,key);
else if(root.val<key) root.right = deleteNode(root.right,key);
else { if(root.left==null) return root.right;
if(root.right==null) return root.left; TreeNode rightmin = findmin(root.right);
root.val = rightmin.val;
root.right = deleteNode(root.right,root.val);
}
return root;
}
private TreeNode findmin(TreeNode root){
while(root.left!=null)
root=root.left;
return root;
} }
 

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

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

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

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

  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. nodejs系列笔记01---Buffer

    纯JavaScript无法处理二进制数据,buffer就是用来处理二进制数据的 原始数据保存在buffer实例中,一个buffer实例类似于数组.buffer的大小在建立时指定的不可更改. buffe ...

  2. SQL宝典

    SQL Server 数据库的高级操作 (1) 批处理 (2) 变量 (3) 逻辑控制 (4) 函数 (5) 高级查询 */ (1)批处理 将多条SQL语句作为一个整体去编译,生成一个执行计划,然后, ...

  3. python - 判断是否为正小数和正整数

    判断输入的金额是否为正整数和正小数 def check_float(string): #支付时,输入的金额可能是小数,也可能是整数 s = str(string) if s.count('.') == ...

  4. c#检查网络文件是否存在

    public bool IsExist(string uri) { HttpWebRequest req = null; HttpWebResponse res = null; try { req = ...

  5. Axure9 v9.0.0.3629 ~ v9.0.0.3633 授权密钥 【2019.02.05】

    现在提供一个支持v9.0.0.3629.v9.0.0.3630.v9.0.0.3631.v9.0.0.3632.v9.0.0.3633的授权码(后续的Beta更新版本应该能继续使用) 被授权人:zd4 ...

  6. iOS -转载-开发之个人开发者账号转公司开发者账号

    ps  :  个人开发者账号升级公司开发者账号的话需要账号开启双重认证,没有开启的话需要开启(不然走到可以升级的那步的话,点击update升级会提示为了安全起见需要账号开启双双重认证,反正我走到upd ...

  7. JavaScript关于闭包

    在学习JavaScript这条路上,对于闭包这个JS中极其重要的应用技巧或者说是一个语言特性一直停留在最最表层的: 函数α内部的函数β被函数外部所调用,然后内部的函数β因为被调用使得其生存周期得以延长 ...

  8. 简单的php基于curl的反向代理程序

    起因: 经理:需要实现一个反向代理? 我:  简单,nginx分分钟配置好. 经理:嗯?没有nginx? 我: nodejs也行啊,网上有例子分分钟搞定. 经理:嗯?只有虚拟主机,只能上传php程序? ...

  9. knowledgeroot 的配置与优化

    首先下载 KnowledgeRoot 的安装包,就是一个压缩文件,解压缩后放到 WebRoot 下面 在浏览器中打开网站,自动提示进行安装,安装的过程很简单,安装结束后即可以使用. 安装包创建的数据库 ...

  10. 阿里面试经历JAVA总结

    为记录阿里的电面经历,特与大家分享,岗位是JAVA研发工程师. 一面主要问题如下: 1)首先自我介绍 2)数据结构算法的基本问题,如排序算法,二叉树遍历,后序遍历非递归,图的最短路径问题 3)对一个数 ...