当前位置:网站首页>Leetcode 450. 删除二叉搜索树中的节点
Leetcode 450. 删除二叉搜索树中的节点
2022-06-11 16:31:00 【von Libniz】
链接:https://leetcode.cn/problems/delete-node-in-a-bst/
二叉搜索树的增删改查都是必会的内容,但是删除相对于其他操作较难一点,可以通过这道题来进行练习。
题目描述
给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
递归解法
class Solution:
""" 递归解法 """
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
""" 删除根为root的BST中node.val等于key的节点,返回删除后的根节点(根节点可能变化) """
if not root:
return None
if key < root.val:
root.left = self.deleteNode(root.left, key)
elif root.val < key:
root.right = self.deleteNode(root.right, key)
# # root可能有0或1个孩子节点,但是处理方式相同,返回孩子节点
elif not root.left or not root.right:
child = root.left if root.left else root.right
return child
else:
successor = root.right # 使用后继节点代替root
while successor.left:
successor = successor.left
root.val = successor.val # root.val赋值为后继节点值
root.right = self.deleteNode(root.right, successor.val)
return root
迭代解法
迭代解法基本思路与上相同,就是要多分类讨论删除的节点是不是根节点。
class Solution:
""" 非递归解法 """
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
if not root:
return root
node, father = self.findNode(root, key)
if not node:
return root
return self.delete(root, father, node)
def delete(self, root, father, node):
if node.left and node.right:
suc_father, successor = self.findSuccessor(node)
node.val = successor.val
return self.delete(root, suc_father, successor)
if not node.left and not node.right:
if root == node:
return None
if father.left == node:
father.left = None
else:
father.right = None
return root
child = node.left if node.left else node.right
if root == node:
return child
if father.left == node:
father.left = child
else:
father.right = child
return root
def findNode(self, root, key):
if not root:
return root
if root.val == key:
return root, None
father = root
while root:
if root.val == key:
return root, father
elif key < root.val:
father = root
root = root.left
else:
father = root
root = root.right
return None, None
def findSuccessor(self, root):
successor = root.right
father = root
while successor.left:
father = successor
successor = successor.left
return father, successor
边栏推荐
- 瑞吉外卖项目(三)员工管理业务开发
- Interview high frequency algorithm question --- longest palindrome substring
- 美团获得小样本学习榜单FewCLUE第一!Prompt Learning+自训练实战
- VLAN partition and routing between VLANs
- leetcode417. 太平洋大西洋水流问题(中等)
- How unittest knows the execution time of each test case
- 什么是rs邮票纸?
- solr(二)solr添加core以及依赖包路径
- List和Set存取元素的差异
- After reading the book reading methods
猜你喜欢

Web page design example assignment -- Introduction to Henan cuisine (4 pages) web final assignment design web page_ Dessert and gourmet college students' web design homework finished product

Laravel listening mode

【pytest学习】pytest 用例执行失败后其他不再执行

面试高频算法题---最长回文子串

面试经典题目:怎么做的性能测试?【杭州多测师】【杭州多测师_王sir】

leetcode463. 岛屿的周长(简单)

Interview high frequency algorithm question --- longest palindrome substring
![Interview classic question: how to do the performance test? [Hangzhou multi surveyors] [Hangzhou multi surveyors \wang Sir]](/img/ea/2c5b48b08a9654b61694b93a2e7d0a.png)
Interview classic question: how to do the performance test? [Hangzhou multi surveyors] [Hangzhou multi surveyors \wang Sir]

PyQt5 使QPlainTextEdit控件支持行号显示

R1 Quick Open Pressure Vessel Operation test Library and Simulation Test in 2022
随机推荐
Time processing logic for the last 7 days, the last 10 days, and the last 90 days
JDBC debugging error, ask for guidance
leetcode417. Pacific Atlantic current problems (medium)
If you want to learn ArrayList well, it is enough to read this article
问题 AC: 中国象棋中的跳马问题
Zhenxiang, Huawei gives n+1 for voluntary resignation
TC8:UDP_ MessageFormat_ 01-02
什么是泛型?为什么要使用泛型?泛型怎么用?那包装类呢?
jdbc调试错误,求指导
Web page design example assignment -- Introduction to Henan cuisine (4 pages) web final assignment design web page_ Dessert and gourmet college students' web design homework finished product
MySQL quick start instance (no loss)
Simulated 100 questions and simulated examination for main principals of hazardous chemical business units in 2022
Interview high frequency algorithm question --- longest palindrome substring
Laravel 8 uses passport for auth authentication and token issuance
项目经理如何击退被工作汇报支配的恐惧感?
How can the project manager repel the fear of being dominated by work reports?
最近7天,最近10天,最近90天时间处理逻辑
leetcode417. 太平洋大西洋水流问题(中等)
数据库全量SQL分析与审计系统性能优化之旅
RSP: An Empirical Study of remote sensing pre training