当前位置:网站首页>[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 .
边栏推荐
- 线性表的查找
- 【达梦数据库】添加自动收集统计信息的任务
- Appx代码签名指南
- Principle of attention mechanism
- Jerry's broadcast has built-in flash prompt tone to control playback pause [chapter]
- 密码学系列之:在线证书状态协议OCSP详解
- Lab1 configuration script
- About Confidence Intervals
- SQL中删除数据
- R data analysis: how to predict Cox model and reproduce high score articles
猜你喜欢
U.S. Air Force Research Laboratory, "exploring the vulnerability and robustness of deep learning systems", the latest 85 page technical report in 2022
【安全的办公和生产力应用程序】上海道宁为您提供ONLYOFFICE下载、试用、教程
Ubuntu20 installation redisjson record
Restcloud ETL Community Edition June featured Q & A
Not All Points Are Equal Learning Highly Efficient Point-based Detectors for 3D LiDAR Point
Variables, process control and cursors (MySQL)
如何自定义Latex停止运行的快捷键
24. (ArcGIS API for JS) ArcGIS API for JS point modification point editing (sketchviewmodel)
美国空军研究实验室《探索深度学习系统的脆弱性和稳健性》2022年最新85页技术报告
Appx code signing Guide
随机推荐
图形化工具打包YOLOv5,生成可执行文件EXE
Set static IP for raspberry pie
VHDL implementation of arbitrary size matrix multiplication
Huawei and Xiaomi "copy each other"
[Dameng database] after backup and recovery, two SQL statements should be executed
Make (convert) ICO Icon
[dream database] add the task of automatically collecting statistical information
Jerry's ble exiting Bluetooth mode card machine [chapter]
About Tolerance Intervals
Shell programming basics
The version control of 2021 version is missing. Handling method
[colmap] 3D reconstruction with known camera pose
21. (article ArcGIS API for JS) ArcGIS API for JS rectangular acquisition (sketchviewmodel)
小程序能运行在自有App中,且实现直播和连麦?
Jericho is in non Bluetooth mode. Do not jump back to Bluetooth mode when connecting the mobile phone [chapter]
Restcloud ETL Community Edition June featured Q & A
Netperf and network performance measurement
Matlab Error (Matrix dimensions must agree)
[safe office and productivity application] Shanghai daoning provides you with onlyoffice download, trial and tutorial
大白话高并发(二)