当前位置:网站首页>Leetcode 450. Deleting a node in a binary search tree
Leetcode 450. Deleting a node in a binary search tree
2022-06-11 16:42:00 【von Libniz】
link :https://leetcode.cn/problems/delete-node-in-a-bst/
The addition, deletion, modification and query of binary search tree are necessary contents , But deleting is a little more difficult than other operations , You can practice with this question .
Title Description
Given the root node of a binary search tree root And a value key, Delete the key Corresponding node , And keep the property of binary search tree unchanged . Back to binary search tree ( May be updated ) Reference to the root node of .
The recursive method
class Solution:
""" The recursive method """
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
""" Delete root as root Of BST in node.val be equal to key The node of , Returns the deleted root node ( Root node may change ) """
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 There may be 0 or 1 A child node , But the treatment is the same , Return to the child node
elif not root.left or not root.right:
child = root.left if root.left else root.right
return child
else:
successor = root.right # Use successor nodes instead of root
while successor.left:
successor = successor.left
root.val = successor.val # root.val Assign values to subsequent nodes
root.right = self.deleteNode(root.right, successor.val)
return root
Iterative method
The basic idea of iterative solution is the same as above , This is to discuss whether the deleted node is the root node in multiple categories .
class Solution:
""" Non recursive 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
边栏推荐
- 虚拟局域网划分与虚拟局域网间路由(VLAN)
- Go语言之Go 快速入门篇(一):第一个 Go 程序
- Learn about Prometheus from 0 to 1
- Analysis report on sales status and supply and demand prospects of phosphoric acid fuel cell industry in the world and China 2022-2028 Edition
- leetcode463. Perimeter of the island (simple)
- Weekly recommended short video: rookie CEO talks about the new logistics track in the future
- 微信小程序时间戳转化时间格式+时间相减
- jsp页面初始加载方式
- tornado环境搭建及基本框架搭建——熟悉的hello world
- Composition of JVM
猜你喜欢

2022 high altitude installation, maintenance and demolition test simulation 100 questions and online simulation test

leetcode684. Redundant connection (medium)

Analysis report on the "fourteenth five year plan" proposal and innovation environment of global and Chinese sodium pyrophosphate industry (2022-2028)

Using MATLAB and dcraw to process digital camera raw files

数据库全量SQL分析与审计系统性能优化之旅

为什么芯片设计也需要「匠人精神」?

2022年R1快开门式压力容器操作考试题库及模拟考试

Analysis of time complexity and space complexity
![[sword finger offer] 21 Adjust array order so that odd numbers precede even numbers](/img/ba/8fa84520bacbc56ce7cbe02ee696c8.png)
[sword finger offer] 21 Adjust array order so that odd numbers precede even numbers

leetcode684. 冗余连接(中等)
随机推荐
Kernel density estimation (2D, 3D)
(验证文件)validateJarFile...报错
485天,我远程办公的 21 条心得分享|社区征文
2022 high altitude installation, maintenance and demolition test simulation 100 questions and online simulation test
Go语言之Go 快速入门篇(一):第一个 Go 程序
消息队列-推/拉模式学习 & ActiveMQ及JMS学习
Pytest测试框架基础篇
每周推荐短视频:菜鸟CEO谈未来物流新赛道
Report on the operation situation and future prospects of China's gear oil industry (2022-2028)
Weekly recommended short video: rookie CEO talks about the new logistics track in the future
Leetcode 1974. 使用特殊打字机键入单词的最少时间(可以,一次过)
^31原型面试题
多任务学习经典品读:MMoE模型篇
How to store tree structure in database
Regression prediction | realization of RBF RBF neural network with multiple inputs and single output by MATLAB
“is-a”,“has-a”,“like-a”
如何把树结构存储到数据库
2022 high voltage electrician special operation certificate examination question bank and online simulation examination
Analysis report on future development trend and investment suggestions of global and Chinese soybean protein industry 2022-2028
药物评价指标