思路:

(1)如果root为空,返回

(2)如果当前结点root是待删除结点:

a:root是叶子结点,直接删去即可

    b:root左子树不为空,则找到左子树的最大值,即前驱结点,使用前驱结点代替待删除的root结点值,并在root的左子树中,继续删除前驱结点

    c:root右子树不为空,则找到右子树的最大值,即后继结点,使用后继结点代替待删除的root结点值,并在root的右子树中,继续删除后继结点

(3)如果待删除值大于root->val,则在右子树中继续删除待删除值

(4)如果待删除值小于root->val,则在左子树中继续删除待删除值

void deleteTreeNode(TreeNode* &root,int key)//使用引用,以可以直接改变原树的结点
{
if(root==NULL) return;
if(root->val==key)
{
if(!root->left && !root->right)
{
root=NULL;
}
else if(root->left)//左子树不空就优先左子树吧
{
//找左子树的最大元素,,即找到前驱结点
TreeNode* cur=root->left;
while(cur->right)
cur=cur->right;
root->val=cur->val;//使用前驱结点覆盖目标结点,并在目标结点的左子树中删除前驱结点
deleteTreeNode(root->left,cur->val);
}
else if(root->right)//右子树不空就优先左子树吧
{
//找右子树的最大元素,,即找到后继结点
TreeNode* cur=root->right;
while(cur->left)
cur=cur->left;
root->val=cur->val;//使用后继结点覆盖目标结点,并在目标结点的右子树中删除后继结点
deleteTreeNode(root->right,cur->val);
}
}
else if(key < root->val)
{
deleteTreeNode(root->left,key);
}
else if(key > root->val)
{
deleteTreeNode(root->right,key);
}
}

要注意,上面递归的过程中使用的是root的引用,因为是要直接删除结点,而不是改变结点中的值。

Leetcode450. 删除二叉搜索树中的节点的更多相关文章

  1. [Swift]LeetCode450. 删除二叉搜索树中的节点 | 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 ...

  2. Java实现 LeetCode 450 删除二叉搜索树中的节点

    450. 删除二叉搜索树中的节点 给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变.返回二叉搜索树(有可能被更新)的根节点的引 ...

  3. [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. Retur ...

  4. [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 ...

  5. 给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用

    一般来说,删除节点可分为两个步骤: 首先找到需要删除的节点: 如果找到了,删除它. 说明: 要求算法时间复杂度为 O(h),h 为树的高度. 示例: root = [5,3,6,2,4,null,7] ...

  6. 450 Delete Node in a BST 删除二叉搜索树中的结点

    详见:https://leetcode.com/problems/delete-node-in-a-bst/description/ C++: /** * Definition for a binar ...

  7. LeetCode 230. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)

    230. 二叉搜索树中第K小的元素 230. Kth Smallest Element in a BST 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的 ...

  8. 刷题-力扣-230. 二叉搜索树中第K小的元素

    230. 二叉搜索树中第K小的元素 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/kth-smallest-element-in-a ...

  9. [Swift]LeetCode701. 二叉搜索树中的插入操作 | Insert into a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

随机推荐

  1. angular cli http请求封装+拦截器配置+ 接口配置文件

    内容:接口配置文件.http请求封装 .拦截器验证登录 1.接口配置文件 app.api.ts import { Component, OnInit } from '@angular/core'; / ...

  2. Kali Linux configuration "Ettercap"

    Xx_Instroduction Ettercap is a man-in-the-middle attack(MITM) tool,kali take this tool,so,use front ...

  3. JAVA工程师技能要求

    近期做了个JAVA工程师分类, JAVA工程师可能是市场上最多类的程序员:   初级JAVA工程师的基本要求 Good basic programming skills 良好基本编程技能 Founda ...

  4. ORA-27468: ""."" is locked by another process

    You have a scheduler job that generated an error. When the error occurred, you attempted to disable ...

  5. redis cluster集群动态伸缩--删除主从节点

    目标:从集群中剔除一组主从(5007,5008) 经过上一节增加5007,5008主从服务节点后,目前集群的情况是这样的: b3363a81c3c59d57143cd3323481259c044e66 ...

  6. 一步一步创建聊天程序2-利用epoll来创建简单的聊天室

    如图,这个是看视频时,最后的作业,除了客户端未使用select实现外,其它的要求都有简单实现. 服务端代码如下: #include <stdio.h> #include <strin ...

  7. [考试反思]1113csp-s模拟测试114:一梦

    自闭.不废话.写一下低错. T1:觉得信心赛T1不会很恶心一遍过样例直接没对拍(其实是想写完T2之后回来对拍的) 状态也不好,基本全机房都开始码了我还没想出来(skyh已经开T2了).想了40多分钟. ...

  8. adb devices无法连接mumu模拟器

    解决方案: 如果你的android环境能够直接访问 adb 的相关指令.只需要把mumu模拟器打开 然后打开cmd -> 输入 adb connect 127.0.0.1:7555 就能直接连上 ...

  9. java获取月的第一天和最后一天

    在Java中获取月的第一天和最后一天主要是通过Calendar对象来实现. /** * 获取月的第一天 * * @param month 月 */ private String getMonthBeg ...

  10. HelloDjango 第 11 篇:自动生成文章摘要

    作者:HelloGitHub-追梦人物 文中涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 博客文章的模型有一个 excerpt 字段,这个字段用于存储文章的摘要.目前为止,还只 ...