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. 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
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道怎么删除:把节点的val替换一下就行了
一位要用list添加,结果它是直接把子树存起来了。
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
先确定点的位置并进行递归,替换值后继续在右边递归
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 忘记写退出条件了,但树的题首先第一步就应该写退出条件root == null
- 替换的情况 必须要把dc和root节点连起来
[二刷]:
- bst中一个数只出现一次,所以继续递归的时候key改成root.val
- 左右节点为空都属于空的情况,要写在一起
[三刷]:
- 先是recursive找key的位置,找到key的位置之后再进行替换
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
删除节点就是把节点值替换一下就行了,剩下的用dc补上来
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
//exit if null
if (root == null) return null;
//define the recursion's location
if (root.val < key) {
root.right = deleteNode(root.right, key);
} else if (root.val > key) {
root.left = deleteNode(root.left, key);
}else {
if (root.left == null) return root.right;
else if (root.right == null) return root.left;
//find min and replace
TreeNode minNode = findMin(root.right);
root.val = minNode.val;
//continue recursion
root.right = deleteNode(root.right, root.val);
}
return root;
}
public TreeNode findMin(TreeNode root) {
while (root.left != null) {
root = root.left;
}
return root;
}
}
450. Delete Node in a BST 删除bst中的一个节点的更多相关文章
- 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...
- 【剑指offer】删除链表中重复的节点,C++实现(链表)
0.简介 本文是牛客网<剑指offer>笔记. 1.题目 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针.例如,链表1-> ...
- C#如何删除数组中的一个元素
C#如何删除数组中的一个元素,剩余的元素组成新数组,数组名不变double[] arr = new double[n];需要删除的是第m+1个数据arr[m]求新数组arr.(新数组arr包含n-1个 ...
- Java-Runoob-高级教程-实例-字符串:03. Java 实例 - 删除字符串中的一个字符
ylbtech-Java-Runoob-高级教程-实例-字符串:03. Java 实例 - 删除字符串中的一个字符 1.返回顶部 1. Java 实例 - 删除字符串中的一个字符 Java 实例 以 ...
- php实现删除链表中重复的节点
php实现删除链表中重复的节点 一.总结 二.php实现删除链表中重复的节点 题目描述: 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表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 ...
- 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 ...
- 剑指offer——面试题18.1:删除链表中重复的节点
// 面试题18(二):删除链表中重复的结点 // 题目:在一个排序的链表中,如何删除重复的结点?例如,在图3.4(a)中重复 // 结点被删除之后,链表如图3.4(b)所示. #include &l ...
- 第18题:在O(1)时间删除链表结点+删除链表中重复的节点
题目描述:题目描述在O(1)时间删除链表结点 给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点. 考查创新编程能力. 思路: 1.如果从头到尾遍历,时间O(n) 2.如果将待删 ...
随机推荐
- PythonStudy——文件操作 File operation
# 文件:就是硬盘的一块存储空间 # 1.使用文件的三步骤: # 打开文件- 得到文件对象:找到数据存放在硬盘的位置,让操作系统持有该空间,具有操作权# 硬盘空间 被 操作系统持有# 文件对象f 被 ...
- 什么是pytorch(4.数据集加载和处理)(翻译)
数据集加载和处理 这里主要涉及两个包:torchvision.datasets 和torch.utils.data.Dataset 和DataLoader torchvision.datasets是一 ...
- zookeeper启动时报Cannot open channel to X at election address Error contacting service. It is probably not running.
配置storm集群的时候出现如下异常: 2016-06-26 14:10:17,484 [myid:1] - WARN [SyncThread:1:FileTxnLog@334] - fsync-in ...
- find查找文件的时间问题
很多细节方面的东西没有到真正用的时候,是觉察不出来的,因为这个时间的问题出了问题,现在好好理一下,这个find的时间很容易就搞混了,一段时间不用,也忘了,也反映出来了自己的基础知识不是很牢固啊 f ...
- 1.2.2 Excel中手机号或身份证号批量加密星号
在对应的单元格中我们输入公式: =LEFT(C4,3)&"****"&RIGHT(C4,4)或=MID(C4,1,3)&"****"&a ...
- EF LIKE 查询
<!-- CSDL content --> <edmx:ConceptualModels> <Schema Namespace="Model" Ali ...
- 解压zipfile & tarfile
def __un_zip(self, file_path): """解压.zip格式文件到同名目录下,若解压之前就存在该目录说明已解压,跳过解压过程,返回该目录" ...
- Python3网络爬虫(四):使用User Agent和代理IP隐藏身份《转》
https://blog.csdn.net/c406495762/article/details/60137956 运行平台:Windows Python版本:Python3.x IDE:Sublim ...
- DevExpress的42种窗体样式
在Winform环境下DevExpress标题栏皮肤 第一步:引用DLL文件,安装DevExpress后在引用>程序集>扩展: DevExpress.BonusSkins.v12.2.dl ...
- 6-安装hbase
1.前提条件 ①安装zookeeper ②安装hdfs ③由于Hbase对时间的要求比较苛刻,所以3台节点时间必须同步,否则会报错 s10 zookeeper RegionServer HMaster ...