当前位置:网站首页>力扣解法汇总450-删除二叉搜索树中的节点
力扣解法汇总450-删除二叉搜索树中的节点
2022-06-12 02:03:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
一般来说,删除节点可分为两个步骤:
首先找到需要删除的节点;
如果找到了,删除它。
示例 1:
输入:root = [5,3,6,2,4,null,7], key = 3
输出:[5,4,6,2,null,null,7]
解释:给定需要删除的节点值是 3,所以我们首先找到 3 这个节点,然后删除它。
一个正确的答案是 [5,4,6,2,null,null,7], 如下图所示。
另一个正确答案是 [5,2,6,null,4,null,7]。
示例 2:
输入: root = [5,3,6,2,4,null,7], key = 0
输出: [5,3,6,2,4,null,7]
解释: 二叉树不包含值为 0 的节点
示例 3:
输入: root = [], key = 0
输出: []
提示:
节点数的范围 [0, 104].
-105 <= Node.val <= 105
节点值唯一
root 是合法的二叉搜索树
-105 <= key <= 105
进阶: 要求算法时间复杂度为 O(h),h 为树的高度。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/delete-node-in-a-bst
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 首先最简单的方式肯定是把二叉树转换成队列,然后重新排序就好了。这样肯定符合,但是时间复杂度较高,也不是本题考察的点。 * 先根据二叉树的性质,找到key对应的节点,然后找到这个节点下,右侧的节点中子节点里面最左侧的那一个替换当前结点。然后找到这个目标节点后,我们还要把其parent对其的引用干掉。
代码:
public class Solution450 {
public TreeNode deleteNode(TreeNode root, int key) {
TreeNode recursion = recursion(root, key);
return recursion;
}
private TreeNode recursion(TreeNode current, int key) {
if (current == null) {
return null;
}
if (current.val == key) {
if (current.right != null) {
TreeNode left = current.left;
TreeNode right = current.right;
TreeNode node = findNode(current, current.right);
node.left = left;
if (node != right) {
node.right = right;
}
return node;
}
return current.left;
}
if (current.val > key) {
current.left = recursion(current.left, key);
} else {
current.right = recursion(current.right, key);
}
return current;
}
private TreeNode findNode(TreeNode parent, TreeNode treeNode) {
if (treeNode.left != null) {
return findNode(treeNode, treeNode.left);
}
if (treeNode.right != null) {
parent.left = treeNode.right;
} else {
parent.left = null;
}
return treeNode;
}
}边栏推荐
- Advantages of Google ads
- SQL calculates KS, AUC, IV, psi and other risk control model indicators
- Introduction to SVM
- 自适应搜索广告有哪些优势?
- ozzanimation-基于sse的动作系统
- PHP security development 13 column module of blog system
- Don't miss it! Five large data visualization screens that HR must collect
- Huawei intermodal game or application review rejected: the application detected payment servicecatalog:x6
- 超图倾斜数据合并根节点后转3dtiles
- Modification of system module information of PHP security development 12 blog system
猜你喜欢

Is the bidding price fixed for each click?

Huawei, this is too strong

Point cloud perception algorithm interview knowledge points (II)

Redis cluster + sentinel mode + replicas

ozzanimation-基于sse的动作系统

kali安装empire过程中遇到的各种报错解决方案

C language programming classic games - minesweeping

如何让杀毒软件停止屏蔽某个网页?以GDATA为例

Subject knowledge and educational ability of information technology in the first half of 2019 – subjective questions

决定广告质量的三个主要因素
随机推荐
螺旋矩阵(技巧)
How to restore the redis cluster and retain the complete cluster data after changing the node IP
Design practice of rongyun Im on electron platform
如何定位关键词使得广告精准投放。
Ozzanmation action system based on SSE
websocket 切后台10秒后 关闭掉了
Niuke monthly race 14- simple counting
PHP security development 13 column module of blog system
Is there a female Bluetooth headset suitable for girls? 38 Bluetooth headsets worth getting started
virsh创建/关闭/停止虚拟机常用的几条指令
SQL calculates KS, AUC, IV, psi and other risk control model indicators
Implementation scheme of iteration and combination pattern for general tree structure
MySQL高级部分知识点
php安全开放10文章列表模块的分页编写
Wide match modifier symbol has been deprecated, do not use
State owned assets into shares, has Jianye real estate stabilized?
Smartbi helps you solve the problem of losing high-value customers
为什么我们要使用谷歌搜索广告?
MySQL表常用操作思维导图
决定广告质量的三个主要因素