当前位置:网站首页>[leetcode] 450 and 98 (deletion and verification of binary search tree)
[leetcode] 450 and 98 (deletion and verification of binary search tree)
2022-07-07 03:33:00 【Learn a little every day】
Search and insert of binary search tree The second part is the deletion and verification of binary search tree
This paper introduces how to use the characteristics of binary search tree to realize 「 Delete 」 and 「 verification 」 operation .
450. Delete nodes in binary search tree
solution : recursive
Different from the deletion of ordinary binary tree , The binary search tree should adjust the tree nodes after deleting the corresponding nodes , Restore binary search tree . There are three cases of deleting nodes :
- The left subtree is empty . The root node of the right subtree replaces the current node .
- Left subtree is not empty , The right subtree is empty . The root node of the left subtree replaces the current node .
- The left and right subtrees are not empty . Use the right subtree minimum node ( Leftmost son ) To replace , And delete the minimum node .
class Solution:
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
if not root:
return None
if root.val == key:
# situation 1
if not root.left:
return root.right
# situation 2
if not root.right:
return root.left
# situation 3
# Find the smallest node of the right subtree
minNode = self.getMin(root.right)
# Delete the smallest node from the right subtree
root.right = self.deleteNode(root.right, minNode.val)
# Replace the current node , Complete the delete operation
root.val = minNode.val
elif root.val > key:
# Enter the left subtree to search
root.left = self.deleteNode(root.left, key)
else:
# Enter the right subtree to search
root.right = self.deleteNode(root.right, key)
return root
# Find the minimum node of the right subtree
def getMin(self, node):
while node.left:
node = node.left
return node
98. Verify binary search tree
solution : In the sequence traversal
Binary search tree (BST) The middle order traversal of is an ordered result , This can be used to determine whether the current tree is a correct binary search tree . The specific performance is to maintain a variable to store the previous calendar value , Judge whether the current node value is greater than the traversal , If more than , Update variable information , Otherwise return to False, Indicates that the subtree of the current node is not a binary search tree .
In the root The node is a subtree of the root node , The determination process of whether it is a binary search tree is as follows ( Any node 「 What to do 」:
- First determine whether the left subtree is a binary search tree .
- Determine whether the right subtree is a binary search tree .
- Judge whether the current element meets BST nature
- If the above conditions are met , with root The subtree with borrowing as the root node is a binary search tree , return True.
class Solution:
def isValidBST(self, root: TreeNode) -> bool:
maxValue = float("-inf")
def traversal(root):
nonlocal maxValue
if not root:
return True
# Determine whether the left subtree is BST
left = traversal(root.left)
if root.val <= maxValue:
return False
maxValue = root.val
# Judge whether the right subtree is BST
right = traversal(root.right)
# All for True, Indicates that root The subtree with borrowing as the root node is BST.
return left and right
return traversal(root)
summary
1、 If the current node will be down ⾯ Of ⼦ Nodes have an overall impact , You can add ⻓ parameter list , Passing information by means of parameters .
2、 stay ⼆ Fork tree recursion framework , Extended out ⼀ set BST The code framework :
3. According to the code framework BST Add, delete, check and modify .
边栏推荐
- Graphical tools package yolov5 and generate executable files exe
- Basic concepts of Huffman tree
- ubuntu20安裝redisjson記錄
- VHDL implementation of single cycle CPU design
- 【安全的办公和生产力应用程序】上海道宁为您提供ONLYOFFICE下载、试用、教程
- 我的勇敢对线之路--详细阐述,浏览器输入URL发生了什么
- 线性表的查找
- Lab1 configuration script
- 21. (article ArcGIS API for JS) ArcGIS API for JS rectangular acquisition (sketchviewmodel)
- Search of linear table
猜你喜欢
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
Create applet from 0
海思万能平台搭建:颜色空间转换YUV2RGB
华为小米互“抄作业”
如何替换模型的骨干网络(backbone)
About Confidence Intervals
23. (ArcGIS API for JS) ArcGIS API for JS ellipse collection (sketchviewmodel)
Basic concepts of Huffman tree
函数重入、函数重载、函数重写自己理解
Restcloud ETL Community Edition June featured Q & A
随机推荐
体会设计细节
19. (ArcGIS API for JS) ArcGIS API for JS line acquisition (sketchviewmodel)
大白话高并发(二)
23.(arcgis api for js篇)arcgis api for js椭圆采集(SketchViewModel)
Restcloud ETL Community Edition June featured Q & A
How to customize the shortcut key for latex to stop running
Cocos2d-x box2d physical engine compilation settings
RestClould ETL 社区版六月精选问答
我的勇敢对线之路--详细阐述,浏览器输入URL发生了什么
ubuntu20安裝redisjson記錄
VHDL实现单周期CPU设计
sshd[12282]: fatal: matching cipher is not supported: aes256- [email protected] [preauth]
input_ delay
装饰设计企业网站管理系统源码(含手机版源码)
数学归纳与递归
VHDL implementation of arbitrary size matrix multiplication
24.(arcgis api for js篇)arcgis api for js点修改点编辑(SketchViewModel)
VHDL实现任意大小矩阵加法运算
Netperf and network performance measurement
Sorting operation partition, argpartition, sort, argsort in numpy