[LeetCode] 450. Delete Node in a BST

450. Delete Node in a BST【LeetCode单题讲解系列】 450._Delete_Node_in_a_BST【LeetCode单题讲解系列】 立即下载

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:
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:
在这里插入图片描述

解析

删除给定值得节点

解法1:递归

首先判断根节点是否为空,由于BST的性质,可以递归定位到要删除的节点。当前节点不等于key时,根据大小关系递归调用左右节点。
当前节点就是要删除的节点时,首先判断是否有一个子节点不存在,将root指向存在的子节点;当左右子节点都存在时,在右子树中找到最小值得节点,并将其值赋给当前节点,再递归调用右子树删除其最小节点。

class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if(!root) 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 || !root->right)
                root = root->left ? root->left:root->right;
            else{
                TreeNode* cur = root->right;
                while(cur->left) cur = cur->left;
                root->val = cur->val;
                root->right = deleteNode(root->right, cur->val);
            }
        }
        return root;
    }
};

解法2:迭代

首先构建一个父节点pre以及当前节点cur,找到key的节点位置为cur,如果cur为空返回root,如果pre为空说明删除节点是根节点,调用del函数删除cur,如果删除节点是pre->left,调用del删除cur,并且pre->left=del(cur),反之,pre->right=del(cur)。
下面解释删除函数del(node)。
删除函数是为了删除当前节点,所以当node为叶子结点时,返回NULL;
当node有一个子节点时,返回那个子节点;
当node有两个子节点时,先在右子树中找到最小值节点cur, 并记录其父节点pre,并赋值给node,当pre为node时,node->right = cur->right,反之,pre->right = cur->right;返回node。

class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        TreeNode* cur = root, *pre = NULL;
        while(cur){
            if(cur->val == key) break;
            pre = cur;
            if(cur->val > key)
                cur = cur->left;
            else
                cur = cur->right;
        }
        if(!cur) return root;
        if(!pre) return del(cur);
        if(pre->left && pre->left->val == key) 
            pre->left = del(cur);
        else
            pre->right = del(cur);
        return root;
    }
    
    TreeNode* del(TreeNode* node){
        if(!node->left && !node->right) return NULL;
        if(!node->left || !node->right)  
            return node->left ? node->left : node->right;
        TreeNode* pre = node, *cur = node->right;
        while(cur->left){
            pre = cur;
            cur = cur->left;
        }
        node->val = cur->val;
        if(pre == node)
            node->right = cur->right;
        else
            pre->left = cur->right;
        return node;
    }
};

解法3:通用二叉树

遍历所有节点,然后删除值为key的节点

class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if (!root) return NULL;
        if (root->val == key) {
            if (!root->right) return root->left;
            else {
                TreeNode *cur = root->right;
                while (cur->left) cur = cur->left;
                swap(root->val, cur->val);
            }
        }
        root->left = deleteNode(root->left, key);
        root->right = deleteNode(root->right, key);
        return root;
    }
};

参考

http://www.cnblogs.com/grandyang/p/6228252.html

【WB32库开发】第9章 TIM1高级定时器——输出多路PWM PWM(Pulse Width Modulation)意为脉冲宽度调制,简称脉宽调制 。在工控行业,PWM信号可以用来调节电机转速、调节变频器以及BLDC电机驱动等;在LED照明行业,可以通过PWM来控制LED的亮暗变化;还可以通过PWM信号来控制无源蜂鸣器发出简单的声音等。 WB32也可输出PWM,需配置WB32上的定时器。 在WB32中有4个定时器,其中TIM1为高级定时器,TIMx(2-4)为通用定时器,这四个定时器均挂载在APB1总线上,每个定时器都可以输出4路PWM。 本章需要使用固件库TIM例程 阅读详情

相关推荐

学习stm32H7之路开始(STM32H7系列1) 前期准备,HAL库框架

2020.01.07 几个重要的参考网址 1.电子工程世界网 http://tags.eeworld.com.cn/tags/STM32H7 链接: link. 2.转:https://blog.csdn.net/simon223/article/list/2?t=1 链接: link. ———————————————— 版权声明:本文为优快云博主「Simon223」的原创文章,遵循 CC 4....

kengkengkeng963的博客 3411

LeetCode 450: 删除二叉搜索树中的节点 Delete Node in a BST

题目: 给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可...

爱写Bug 527

STM32--基于STM32F103C8T6的OV7670摄像头显示

使用 STM32F103C8T6 微控制器驱动 OV7670 摄像头模组,并将图像数据显示在屏幕上,是嵌入式系统中常见的图像采集与显示应用。OV7670 是一款 CMOS VGA 图像传感器,具有高灵敏度、低电压等特点,适用于嵌入式应用。而 STM32F103C8T6 是一款常用的 MCU 控制器,具有丰富的管脚资源和较高的工作频率。

duierrorshuobu的博客 2146

【Leetcode】450. Delete Node in a BST

法1:比较简单的解法是通过旋转。如果树根等于key,并且左右子树都空,则返回null,这个情况很简单;否则就把树根向下旋转,如果左子树非空则右旋,否则左旋(因为左子树为空的话是没法做右旋的)。事实上左旋还是右旋怎样都无所谓,最重要的是要把等于key的节点旋到叶子那里,然后就容易删了。法2:更简单的解法是通过合并。如果树根等于key,则直接返回左右子树合并的新树。在二叉搜索树中删除值为key的节点,返回删除后的树根。

数学、算法爱好者的博客 257

java modbus通讯协议_Modbus通 讯 协 议

电磁流量计Modbus通 讯 协 议(版本号:9000-K V9.1.1)2020年01月01日通讯协议针对KFD电磁流量计工业应用设计,版本:9000-K V9.1.1,该版本主要用于实时数据采集、流量测量、流量累计控制及部分参数的修改。一、主机系统通讯部件要求1.国际标准RS-485通讯接口部件,不小于11Bytes 的通信缓冲区(FIFO),支持1200、2400、4800、96...

weixin_39956612的博客 1012

Python声音识别实战:从音频处理到智能分类

本文详细介绍了使用Python进行声音识别的完整实战流程。从音频预处理、特征提取(如MFCC)到构建分类模型(SVM、CNN),并最终实现一个实时环境音分类器。文章以Python为核心工具,结合librosa等库,为开发者提供了从理论到实践的清晰路径,适用于智能家居、工业监测等多种应用场景。

weixin_29064009的博客 324

Arduino 库开发实战:从3个.h/.cpp模块到可复用的自定义库

本文详细介绍了如何将Arduino项目中的.h/.cpp模块转化为可复用的自定义库,涵盖从基础模块封装到标准库结构的完整流程。通过实战示例展示屏幕驱动、传感器读取和数据处理三大核心模块的开发技巧,帮助开发者提升代码复用性和维护效率,适用于各类Arduino工程开发场景。

anqiu4023的博客 415

Android息屏后台任务保活实战:从定时器失效到厂商白名单破解

本文深入探讨了Android应用在息屏后定时任务失效的根源,即系统为省电而触发的休眠机制。文章提供了从传统方案失效分析到实战破解的完整路径,核心在于引导用户将应用加入电池优化白名单并开启自启动权限,从而合法绕过限制。同时,结合WorkManager、AlarmManager等可靠定时方案,实现稳定的后台任务执行。

weixin_28475533的博客 423

【leetcode】-450. Delete Node in a BST删除BST中一个结点

Delete Node in a BST题目递归python 代码 题目 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

。 299

uniapp+google map不显示

uniapp+renderJS+google map开发安卓版APP非小程序,google map小叉号不显示,google map自定义marker右上角的小叉号如何去掉,uniapp+google map不显示

renkai721的专栏 527

H3C交换机命令大全,分模块好记忆!

在深入命令之前,我们先来了解H3C交换机的命令行基础。<H3C>[H3C]常用基础命令如下:这些基础命令是操作H3C交换机的敲门砖,熟练掌握后,我们将按模块深入探讨更具体的配置命令。H3C交换机的命令可以按功能模块分类,包括系统管理、接口配置、VLAN操作、链路聚合、路由配置、安全管理等。

网络技术联盟站 784

leetcode 450 删除二叉搜索树中的节点

细心做。 题: 450. 删除二叉搜索树中的节点 给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。 一般来说,删除节点可分为两个步骤: 首先找到需要删除的节点; 如果找到了,删除它。 说明: 要求算法时间复杂度为 O(h),h 为树的高度。 示例: root = [5,3,6,2,4,null,7] key = 3 5 / \ 3 6 / \ \ 2

AndyJ的学习放弃之旅 708

沁恒CH32V30X学习笔记02--GPIO的使用教程及2次封装驱动

刚复位后,GPIO 口运行在初始状态,这时大多数 IO 口都是运行在状态所有的 GPIO 口都可以被配置外部中断输入通道,但一个外部中断输入通道最多只能映射到一个GPIO 引脚上,且外部中断通道的序号必须和 GPIO 端口的位号一致,,两方都是一对一的关系。复用功能。

Car12 706

LeetCode 450. Delete Node in a BST 题解

本题是二叉搜索树的经典问题,主要考察对二叉搜索树性质的理解和应用。通过使用递归,我们可以高效地在二叉搜索树中删除目标节点。递归的核心思想是:利用二叉搜索树的性质,递归地删除目标节点。当找到目标节点时,根据其是否有子节点的情况进行处理,确保删除后。

cannonjinx的博客 1015

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Leetcode450 Delete Node in a BST

Problem Definition solution 1: Recursive

weixin_45764148的博客 207

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

LeetCode 450. Delete Node in a BST

原题网址:https://leetcode.com/problems/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 up

James Pan 1108

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

[leetcode] 450. Delete Node in a BST

Description 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 ...

Learning from the mistakes 297

leetcode450Delete 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 i

ArthurZhang的专栏 425

[leetcode] 450. Delete Node in a BST 解题报告

题目链接: https://leetcode.com/problems/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 u

小榕流光的专栏 3560

leetcode 450. Delete Node in a BST

题意:删除二分搜索树中值为key的结点 算法思路:当root为null时,直接返回root。首先,用二分搜索法查找到值为key的结点,在查找的过程中记录该结点的父结点并标记该结点是父结点的左孩子还是右孩子(用于保存删除某结点时涉及到的其他结点)。若没有找到key值的结点,则原样返回。若父结点为null,说明删除的结点是根结点,如果 root.left为null,则root指向root.right,...

IT_BOY__的博客 257

[leetcode]450. Delete Node in a BST

题目链接:https://leetcode.com/problems/delete-node-in-a-bst/#/description 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

xiaocong1990的博客 395

LeetCode—450. Delete Node in a BST

LeetCode—450. Delete Node in a BST 题目 https://leetcode.com/problems/delete-node-in-a-bst/ 删除搜索二叉树中的一个节点,返回新的搜索二叉树,因为结果不唯一,任意返回一个即可。 解析及代码 /** * Definition for a binary tree node. * public class Tree...

pnnngchg的博客 221

Leetcode 450 Delete Node in a BST

Problem: 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 div...

Opium_Z的博客 192

LeetCode //C - 450. Delete Node in a BST

【代码】LeetCode //C - 450. Delete Node in a BST。

Made in Code 1197

【leetcode】【medium】450. Delete Node in a 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. Return the root node reference (possibly updated) of the BST. Basically, the ...

lemonade13的博客 208

[LeetCode] 450. Delete Node in a BST @ python

一.题目: 删除二叉树中指定一节点,并调整二叉树,使结果仍是BST。 二.解题思路: 我们应该逐个访问树上的节点,如果某个节点的值正好是目标值,那么: 若它没有右子树,那么直接用它的左孩子替换它; 若它有右子树.那么将它的值和它右子树中的最小节点的值调换。 代码如下: # Definition for a binary tree node. # class TreeNode(object): # ...

tmfighting的博客 252
上一篇: [LeetCode] 437. Path Sum III
下一篇: [LeetCode] 501. Find Mode in Binary Search Tree
Peng_maple
博客等级 码龄10年 0粉丝 79原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值