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

    CROSSCHECK: 确认 rman repository 与 实际的备份集 是否同步. 你可以先使用 LIST 命令查看你之前做的备份的情况, 然后使用 CROSSCHECK 命令来确认这些文件还 ...

  2. 【vijos】1881 闪烁的繁星(线段树+特殊的技巧)

    https://vijos.org/p/1881 这场比赛太难了sad.所以我都没做.. 这题一开始我竟然不会sad(本来就不会),然后我继续yy..yy了好久,竟然yy了个什么可拆分的并查集?(sa ...

  3. uva 465 - Overflow 高精度还是浮点数?

    uva 465 - Overflow  Overflow  Write a program that reads an expression consisting of two non-negativ ...

  4. hdu 4284(状压dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4284 思路:类似于poj3311:http://poj.org/problem?id=3311,首先f ...

  5. JavaScript------日期和时间戳的相互转换

    var date = new Date(); 日期转时间戳 Number(date)或者date.getTime(); //只是转换成了纯数字的时间戳,例如:1498144203861需要转换才能使用 ...

  6. Database Designer

    DBDesigner http://fabforce.net/dbdesigner4/index.php DB Designer Fork http://sourceforge.net/project ...

  7. 会话技术Cookie&Session

    1.会话技术概述 从打开浏览器访问某个站点,到关闭这个浏览器的整个过程,称为一次会话.会话技术用于记录本次会话中客户端的状态与数据. 会话技术分为Cookie和Session: Cookie:数据存储 ...

  8. 认识tornado(五)

    Tornado 自带了模板系统,模板语法与 Django 差异不大.这里简单地介绍如何使用 Tornado 的模板系统. 首先是编写 URL 规则与 Handler: 01 class NowaMag ...

  9. homebrew常用指令

    其它Homebrew指令: brew list   —列出已安装的软件 brew update   —更新Homebrew brew home  *—用浏览器打开 brew info   *—显示软件 ...

  10. md5 算法类 java

    package com.sunyard.p2p.util; import java.security.MessageDigest; import java.security.NoSuchAlgorit ...