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

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

一般来说,删除节点可分为两个步骤:

首先找到需要删除的节点;

如果找到了,删除它。

说明: 要求算法时间复杂度为 O(h),h 为树的高度。

示例:

root = [5,3,6,2,4,null,7]

key = 3

    5
/ \
3 6
/ \ \
2 4 7

给定需要删除的节点值是 3,所以我们首先找到 3 这个节点,然后删除它。

一个正确的答案是 [5,4,6,2,null,null,7], 如下图所示。

    5
/ \
4 6
/ \
2 7

另一个正确答案是 [5,2,6,null,4,null,7]。

    5
/ \
2 6
\ \
4 7
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if (root == null) {
return null;
}
if (key < root.val) {
// 待删除节点在左子树中
root.left = deleteNode(root.left, key);
return root;
} else if (key > root.val) {
// 待删除节点在右子树中
root.right = deleteNode(root.right, key);
return root;
} else {
// key == root.val,root 为待删除节点
if (root.left == null) {
// 返回右子树作为新的根
return root.right;
} else if (root.right == null) {
// 返回左子树作为新的根
return root.left;
} else {
// 左右子树都存在,返回后继节点(右子树最左叶子)作为新的根
TreeNode successor = min(root.right);
successor.right = deleteMin(root.right);
successor.left = root.left;
return successor;
}
}
} private TreeNode min(TreeNode node) {
if (node.left == null) {
return node;
}
return min(node.left);
} private TreeNode deleteMin(TreeNode node) {
if (node.left == null) {
return node.right;
}
node.left = deleteMin(node.left);
return node;
}
}

Java实现 LeetCode 450 删除二叉搜索树中的节点的更多相关文章

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

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

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

  4. Leetcode450. 删除二叉搜索树中的节点

    思路: (1)如果root为空,返回 (2)如果当前结点root是待删除结点: a:root是叶子结点,直接删去即可 b:root左子树不为空,则找到左子树的最大值,即前驱结点,使用前驱结点代替待删除 ...

  5. Leetcode 450.删除二叉搜索树的节点

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

  6. Leetcode:230. 二叉搜索树中第K小的元素

    Leetcode:230. 二叉搜索树中第K小的元素 Leetcode:230. 二叉搜索树中第K小的元素 思路: 利用BST的中序历遍的结果为其排序后的结果,我们可以利用其特性直接找到第k个中序遍历 ...

  7. 【LeetCode】230#二叉搜索树中第K小的元素

    题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明: 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输入: ro ...

  8. 【LeetCode】230. 二叉搜索树中第K小的元素 Kth Smallest Element in a BST

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:算法题,刷题,Leetcode, 力扣,二叉搜索树,BST ...

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

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

随机推荐

  1. 使用 Visual Studio Code 搭建 C/C++ 开发和调试环境

    文章目录 1. 安装 C/C++ 插件 2. 安装 MinGW-w64 并配置好环境变量 3. 测试环境变量是否配置正确 4. 创建和设置 C 语言开发工作区 5. 编写你的第一个 C 语言程序 6. ...

  2. Linux内核驱动学习(一)编写最简单Linux内核模块HelloWorld

    文章目录 准备工作 什么是内核模块 编写 hello.c 模块编译 相关指令 测试结果 模块加载 模块卸载 准备工作 在进行以下操作前,首先我准备了一台电脑,并且安装了虚拟机,系统是Ubuntu16. ...

  3. 力扣题解-面试题10- II. 青蛙跳台阶问题

    题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级台阶.求该青蛙跳上一个 n 级的台阶总共有多少种跳法. 答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008, ...

  4. EM算法和GMM算法的相关推导及原理

    极大似然估计 我们先从极大似然估计说起,来考虑这样的一个问题,在给定的一组样本x1,x2······xn中,已知它们来自于高斯分布N(u, σ),那么我们来试试估计参数u,σ. 首先,对于参数估计的方 ...

  5. Python格式化字符串(格式化输出)

    熟悉C语言 printf() 函数的读者能够轻而易举学会 Python print() 函数,它们是非常类似的. print() 函数使用以%开头的转换说明符对各种类型的数据进行格式化输出,具体请看下 ...

  6. Codeforces1157B(B题)Long Number

    B. Long Number You are given a long decimal number aa consisting of nn digits from 11 to 99. You als ...

  7. 模板渲染jnja2模块

    模板渲染jnja2模块 模板的引入: 在返回动态页面时,上述我们在08版web框架返回每次访问的时间,利用自己写的占位符进行字符串替换进行动态响应: 在实际应用中,完全可以从数据库中读取数据,然后替换 ...

  8. k8s搭建实操记录一(master)

    #1)关闭CentOS7自带的防火墙服务 systemctl disable firewalld systemctl stop firewalld swapoff  -a     ##虚拟机要关闭交换 ...

  9. eslint插件开发教程

    开发eslint插件目的:根据项目需要,自定义满足项目特殊需要的校验规则 参考eslint官方文档展开阐述 插件开发 自定义规则 单元测试 下面开始通过一个示例demo来介绍插件整个开发流程 代码中出 ...

  10. 一个导致JVM物理内存消耗大的Bug

    概述 最近我们公司在帮一个客户查一个JVM的问题(JDK1.8.0_191-b12),发现一个系统老是被OS Kill掉,是内存泄露导致的.在查的过程中,阴差阳错地发现了JVM另外的一个Bug.这个B ...