[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. 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 思路:分两种情况,一种是要删除的节点只有一个孩子,另一种是要删除的节点有两个孩子;
如果只有一个孩子,那么我们让这个节点引用到他非空的孩子上去,即root = root.right或
root = root.left;如果有两个孩子的话,那么有两种办法,一种是找他右边的最小值,另一种
是找他左边的的最大值。将值赋给该节点,然后删除右边最小值或左边最大值;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if (root==null)
return null;
else if (key<root.val)
root.left = deleteNode(root.left,key);
else if (key>root.val)
root.right = deleteNode(root.right,key);
else {
if (root.left!=null&&root.right!=null){
TreeNode tmp = findMin(root.right);
root.val = tmp.val;
root.right = deleteNode(root.right,root.val);
}
else {
TreeNode tmp = root;
if (root.left==null)
root = root.right;
else if (root.right==null)
root = root.left;
tmp = null;
}
}
return root;
}
private TreeNode findMin(TreeNode root){
if (root==null)
return null;
if (root.left==null)
return root;
return findMin(root.left);
}
}
[Leetcode]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二叉搜索树删除节点
二叉树变量只是一个地址 public static void main(String[] args) { TreeNode t = new TreeNode(3); help(t); System.o ...
- 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...
- 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 ...
- 【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. ...
- 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 ...
- 450 Delete Node in a BST 删除二叉搜索树中的结点
详见:https://leetcode.com/problems/delete-node-in-a-bst/description/ C++: /** * Definition for a binar ...
随机推荐
- 用spark导入数据到hbase
集群环境:一主三从,Spark为Spark On YARN模式 Spark导入hbase数据方式有多种 1.少量数据:直接调用hbase API的单条或者批量方法就可以 2.导入的数据量比较大,那就需 ...
- maven+springmvc的配置
1. 首先创建1个mavenweb项目 如果没有的话最好是去官网下载一个最新版本的eclipse 里面什么都有 maven/gradle 啥的 2. 选择路径 没啥影响 就是一个路径 默认就行 ...
- 使用 NLog 给 Asp.Net Core 做请求监控
为了减少由于单个请求挂掉而拖垮整站的情况发生,给所有请求做统计是一个不错的解决方法,通过观察哪些请求的耗时比较长,我们就可以找到对应的接口.代码.数据表,做有针对性的优化可以提高效率.在 asp.ne ...
- Appium+python的环境搭建!
我的电脑64位操作系统 下载并安装所需软件: 1.adb环境 2.下载(python-2.7.12.amd64.msi)并安装python 3.下载并解压adt,如(adt-bundle-window ...
- 自学java能不能找到找到一份java工作。
关于自学Java能不能通过社招找到一份互联网公司Java开发的工作,有无数的人问出这样的问题,答案没有标准的,只能从概率去考虑.有的人可以,有的人不可以,有的人自学就业的概率就是高,有的概率就是低. ...
- 从壹开始微服务 [ DDD ] 之九 ║从军事故事中,明白领域命令验证(上)
烽烟 哈喽大家周二好呀,咱们又见面了,上周末掐指一算,距离 圣诞节 只有 5 周的时间了(如果你还不知道为啥我要提圣诞节这个时间点,可以看看我的第二系列开篇<之一 ║ D3模式设计初探 与 我的 ...
- flask下载excel
flask 应用的基本结构: htmlweb.py -- static -- templates 将 bootstrap.min.css 放到 static 文件夹下,在 templates 文件夹下 ...
- 从0到1:使用Caliburn.Micro(WPF和MVVM)开发简单的计算器
从0到1:使用Caliburn.Micro(WPF和MVVM)开发简单的计算器 之前时间一直在使用Caliburn.Micro这种应用了MVVM模式的WPF框架做开发,是时候总结一下了. Calibu ...
- .NETCore 新型 ORM 功能介绍
简介 FreeSql 是一个功能强大的 .NETStandard 库,用于对象关系映射程序(O/RM),支持 .NETCore 2.1+ 或 .NETFramework 4.6.1+. 定义 IFre ...
- Java虚拟机二:使用jvisualvm工具远程监控tomcat内存
jdk中自带了很多工具可以用于性能分析,位于jdk的bin目录下,jvisualvm工具可以以图形化的方式更加直观的监控本地以及远程的java进程的内存占用,线程状态等信息. 一.配置tomcat 在 ...