思路:

(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. Attach Files to Objects 将文件附加到对象

    In this lesson, you will learn how to attach file collections to business objects. For this purpose, ...

  2. uml统一建模语言学习笔记(一)

    UML是一种统一建模语言,他是以面向对象的方式来实现对任何的系统进行描述的一种语言, 它包括9种图形+包图,分为静态和动态两种,也就是结构图和行为图 “静态”图有:用例图.类图.对象图.部署图.构件图 ...

  3. MYSQL5.7 INDEXES之如何使用索引(一)

    Most MySQL indexes (PRIMARY KEY, UNIQUE, INDEX, and FULLTEXT) are stored in B-trees. Exceptions: Ind ...

  4. Redis基础类型常用操作命令

    Redis基础类型常用操作命令 概念:Redis是用C语言开发的一个开源的高性能键值对数据库. 特征: 数据间没有必然的联系 内部采用单线程机制进行工作 高性能 多数据类型支持 字符串类型 Strin ...

  5. [Go] 实现面向对象中的继承和覆盖方法

    go中的继承是使用结构体嵌套实现的,可以继承父类的方法 覆盖和其他面向对象的语言是一样的,函数名,参数,返回类型一致,就可以覆盖父类的方法 package main import "log& ...

  6. s3c2440裸机-内存控制器(五、SDRAM编程实现)

    配置内存控制器-SDRAM编程配置 2440内存控制器共有13个寄存器. BANK0--BANK5只需要设置BWSCON和BANKCONx(x为0-5)两个寄存器: BANK6.BANK7外接SDRA ...

  7. December 07th, Week 49th Saturday, 2019

    Snowflakes are pretty patterns etched in water's dreams. 雪花,是水在梦中镌刻的美丽图案. From Anthony T.Hincks. Tod ...

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

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

  9. linux远程登入/远程上传文件

    一.远程登入 1.安装 Xshell5 2.查看是否具备连接 在linux 主机上输入 chkconfig --list | grep sshd #sshd 0:关闭 1:关闭 2:启用 3:启用 4 ...

  10. ElementUI项目中怎样引用Jquery

    场景 使用ElementUI的快速开始的项目模板搭建Element项目后, 要在vue页面中使用jquery的语法. 这里直接使用$.ajax会提示$找不到. 注: 博客:https://blog.c ...