当前位置:网站首页>[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 .
边栏推荐
- Open3D 网格滤波
- Not All Points Are Equal Learning Highly Efficient Point-based Detectors for 3D LiDAR Point
- Install torch 0.4.1
- Lost in the lock world of MySQL
- Optimization of application startup speed
- 24. (ArcGIS API for JS) ArcGIS API for JS point modification point editing (sketchviewmodel)
- VHDL implementation of arbitrary size matrix addition operation
- Ubuntu20 installation redisjson record
- ubuntu20安装redisjson记录
- Jerry's transmitter crashed after the receiver shut down [chapter]
猜你喜欢
随机推荐
如何替换模型的骨干网络(backbone)
从0开始创建小程序
数学归纳与递归
VHDL implementation of arbitrary size matrix multiplication
Opencv environment, and open a local PC camera.
Open3d mesh filtering
What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
[cpk-ra6m4 development board environment construction based on RT thread studio]
Jerry's ble exiting Bluetooth mode card machine [chapter]
Matlab Error (Matrix dimensions must agree)
【基于 RT-Thread Studio的CPK-RA6M4 开发板环境搭建】
SSL证书部署
[safe office and productivity application] Shanghai daoning provides you with onlyoffice download, trial and tutorial
枚举通用接口&枚举使用规范
【colmap】已知相机位姿情况下进行三维重建
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
HDU 4337 King Arthur&#39; S Knights it outputs a Hamiltonian circuit
cocos3——8.实现初学者指南
VHDL implementation of arbitrary size matrix addition operation
密码学系列之:在线证书状态协议OCSP详解