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

  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. js实现延迟加载

    defer async.await 动态创建DOM jQ的getScript()方法 window.onload().$(document).ready() Promise setTimeout.se ...

  2. 洛谷 P1396

    P1396 传送门 扯些题外话 讲真的我刚看到这个题的时候真的傻fufu的..... 大体题意 找出从s走到t的拥挤度最大值最小.. 思路 说最大值最小可能就会有dalao开始二分了. 想我这种的蒟蒻 ...

  3. SQL进阶-去重

    一.去重的2种基本方法 1.DISTINCT ## 建表: CREATE TABLE teacher( teacher_id ), teacher_name ), id_no ) ); CREATE ...

  4. vuex实现登录状态的存储,未登录状态不允许浏览

    基础思路就是使用vuex状态管理来存储登录状态(其实就是存一个值,例如token),然后在路由跳转前进行登录状态的判断,可以使用vue-router的全局前置守卫beforeEach,也可以使用路由独 ...

  5. nginx 反向代理之 proxy_set_header

    proxy_set_header用来设定被代理服务器接收到的header信息. 语法:proxy_set_header field value; field :为要更改的项目,也可以理解为变量的名字, ...

  6. 为arm-linux开发板挂载基于nfs的根文件系统

    linux4.14内核,首先设置kernel的bootargs,在make menuconfig中有三种方式来配置: 第一种方式为如果uboot中设置了bootargs环境变量,就采用uboot的bo ...

  7. dubbo简单示例

    dubbo简单示例 2019-09-06 1 Zookeeper注册中心的搭建(windows单机) 下载zookeeper压缩包并解压到 D:\zookeeper\apache-zookeeper- ...

  8. 数位dp技巧

    一个奇怪的东西 正反都能dp!: 正常我们数位dp都是从高到低,以这样的方式保证其小于给定数-> ll n; int num[N],l; ll dp[]; ll dfs(int p,int li ...

  9. JavaScript Date对象和函数 (一)

    JavaScript_Date对象说明  Date中文为"日期"的意思,Date继承自Object对象,此对象提供操作,显示日期与时间的函数 Date对象构造函数 Date对象具有 ...

  10. PHP系列 | PHPexcel导入xls格式 ,提示错误:iconv(): Wrong charset, conversion from `CP936' to `UTF-8' is not allowed

    导入xls格式(2003版本)时会报错提示错误信息 iconv(): Wrong charset, conversion from `CP936' to `UTF-8' is not allowed[ ...