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. Return the root node reference (possibly updated) of the BST.
Basically, the deletion can be divided into two stages:
- Search for a node to remove.
- 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的更多相关文章
- [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 ...
- [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 ...
- 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. ...
- 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 ...
- 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 ...
- 【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 ...
- 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...
- 450 Delete Node in a BST 删除二叉搜索树中的结点
详见:https://leetcode.com/problems/delete-node-in-a-bst/description/ C++: /** * Definition for a binar ...
- [leetcode]450. Delete Node in a BST二叉搜索树删除节点
二叉树变量只是一个地址 public static void main(String[] args) { TreeNode t = new TreeNode(3); help(t); System.o ...
随机推荐
- Tuning 05 Sizing other SGA Structure
Redo Log Buffer Content The Oracle server processes copy redo entries from the user’s memory space t ...
- 扩张js的String——trim
//去掉字符两端的空白字符 String.prototype.Trim=function () { return this.replace(/(^[\t\n\r]*)|([\t\n\r]* ...
- 第二百三十六节,Bootstrap辅组类和响应式工具
Bootstrap辅组类和响应式工具 学习要点: 1.辅组类 2.响应式工具 本节课我们主要学习一下 Bootstrap 的辅组类和响应式工具,辅助类提供了一组类来辅 组页面设计,而响应式工具则利用媒 ...
- 日期类Date
Java在日期类中封装了有关日期和时间的信息,用户可以通过调用相应的方法来获取系统时间或设置日期和时间.Date类中有很多方法在JDK1.0公布后已经过时了,在8.3中我们将介绍JDK1.0中新加的用 ...
- Sql server不同数据类型间拼接(+)
)+'m' 输出 4m 若 +'m' 输出:在将 varchar 值 'm' 转换成数据类型 int 时失败.
- 033 调整数组顺序使奇数位于偶数前面(keep it up)
剑指offer中题目:http://ac.jobdu.com/problem.php?pid=1516 题目描写叙述: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序.使得全部的奇数位于数组的 ...
- excel导出工具类
package com.jianwu.util.excel; import com.google.common.collect.Lists;import com.jianwu.exception.Mo ...
- CodeForces 157A Game Outcome
A. Game Outcome time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Fluent Ribbon 第七步 状态栏
上一节,介绍了StartScreen的主要功能,本节介绍Ribbon的另外一个小功能StatusBar,状态栏是脱离ribbon之外单独存在,可以单独使用的控件 其基本代码定义如下: <Flue ...
- Character Sets, Collation, Unicode :: utf8_unicode_ci vs utf8_general_ci
w Hi, You can check and compare sort orders provided by these two collations here: http://www.collat ...