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

  1. 首先找到需要删除的节点;
  2. 如果找到了,删除它。

说明: 要求算法时间复杂度为 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

  

解:

class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None class Solution:
def deleteNode(self, root: TreeNode, key: int) -> TreeNode:
if not root:
return root
if root.val > key:
root.left = self.deleteNode(root.left, key)
elif root.val < key:
root.right = self.deleteNode(root.right, key)
else:
if not root.left or not root.right:
root = root.left if root.left else root.right
else:
cur = root.right
while cur.left:
cur = cur.left
root.val = cur.val
root.right = self.deleteNode(root.right, cur.val) return root

  

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

  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. 根运动 (Root Motion) – 工作原理

    http://blog.csdn.net/myarrow/article/details/45505085 1. 基本概念 在Unity3D动画中,模型的位置.角度经常会发生变化,我们需要决定是否将模 ...

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

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

  6. [LeetCode]230. 二叉搜索树中第K小的元素(BST)(中序遍历)、530. 二叉搜索树的最小绝对差(BST)(中序遍历)

    题目230. 二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 题解 中序遍历BST,得到有序序列,返回有序序列的k-1号元素. 代 ...

  7. [Swift]LeetCode700. 二叉搜索树中的搜索 | Search in a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...

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

  9. [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素

    题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...

随机推荐

  1. dolt 基于git协议的数据管理工具

    dolt 基于git 协议提供了多版本,分支特性的数据管理工具,使用简单,同时也提供了类似github 的一个云服务 安装 下载地址 https://github.com/liquidata-inc/ ...

  2. ent 基本使用十九 事务处理

    ent 生成的代码中client 提供了比较全的事务处理 启动单个事务进行处理 // GenTx generates group of entities in a transaction. func ...

  3. Vue与REACT两个框架的区别和优势对比

    VUE和REACT两个JavaScript框架都是当下比较受欢迎的,他们两者之间的区别有那些,各自的优缺点是什么,本文将为你呈现. 简单介绍 除非你最近一直不关注前端的发展,不然你肯定听说过由Face ...

  4. 运行java程序的时候出现Exception in thread "main" java.lang.UnsupportedClassVersionError:

    1 Exception in thread "main" java.lang.UnsupportedClassVersionError: com/test/inherited/In ...

  5. html 选择本地图片,显示选择的图片

    主要用的是html5的文件系统部分: <html> <head> <meta name="viewport" content="width= ...

  6. js转义问题

    js转义问题有很多场景,比如常见的根据某个字符串删除或者修改以及将某字符串传递至某个页面. 今天以一个简单的示例代码为例: <html> <head> <meta htt ...

  7. Spring实现多数据源配置

    一.前言 对于小型项目,服务器与数据库是可以在同一台机子上的,但随着业务的庞大与负责,数据库和服务器就会分离开来.同时随着数据量的增大,数据库也要分开部署到多台机子上. 二.Spring配置文件修改 ...

  8. 【小盘子看源码-MyBatis-1】MyBatis配置文件的加载流程

    众所周知,Mybatis有一个全局的配置,在程序启动时会加载XML配置文件,将配置信息映射到org.apache.ibatis.session.Configuration类中,例如如下配置文件. &l ...

  9. Django实现自动发布(3发布-升级和回退)

    发布实际上就是将服务的某个版本和一台主机关联,我用一张表(MicroServiceInstance)记录了主机id.服务id.版本id,目前一台主机只能部署一个版本,所以主机id和服务id要做联合索引 ...

  10. js获取数组中的最大值/最小值

    目录 前言 1. 使用Math的静态方法max/min 1.1 结合ES6的扩展运算符...使用 1.2 结合apply/call方法来使用 1.3 结合reduce来使用 2. 排序获取 2.1 只 ...