当前位置:网站首页>[leetcode] 700 and 701 (search and insert of binary search tree)
[leetcode] 700 and 701 (search and insert of binary search tree)
2022-07-07 03:33:00 【Learn a little every day】
Binary search tree
Binary search tree as a classic data structure , It has the characteristics of quick insertion and deletion of linked list , It also has the advantage of fast array search ; So it's widely used , For example, in the file system and database system, this data structure is generally used for efficient sorting and retrieval operations .
BST Basic operation of : Judge BST Legitimacy 、 increase 、 Delete 、 check .
This paper introduces how to use the characteristics of binary search tree to realize 「 Search for 」 and 「 increase 」 operation .
700. Search in binary search tree

solution : recursive
The search of ordinary binary tree needs to traverse all nodes to judge in turn . For binary search tree , You can take advantage of its small on the left and large on the right , Do something like ⼆ Sub search operation , Improve search ⼀ The efficiency of elements . At this time, for any node 「 What to do 」 Judge as follows :
- If the current element is less than the target value , Then enter the left subtree search
- If the current element is greater than the target value , Then enter the right subtree search
- If equal , The current node is returned
class Solution:
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
if not root:
return None
if root.val == val:
return root
elif root.val > val:
result = self.searchBST(root.left, val)
else:
result = self.searchBST(root.right, val)
return result
701. Insert operation in binary search tree


solution : recursive
Operations on data structures ⽆⾮ Traverse + visit , Traversal is 「 look for 」, The interview is 「 Change 」. Specific to this question , insert ⼊⼀ Number , Is to find the plug first ⼊ Location , Then enter ⾏ insert ⼊ operation .
On ⼀ A question , We summarized BST Traversal framework in , Namely 「 look for 」 The problem of . Direct framing , add 「 Change 」 The operation of .⼀ Dan involves 「 Change 」, The function is going to return TreeNode type , And call recursion ⽤ The return value of ⾏ receive .
class Solution:
def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
if not root:
# Find the insertion location , Make changes
return TreeNode(val)
if root.val > val:
# Receive return value
root.left = self.insertIntoBST(root.left, val)
else:
# Receive return value
root.right = self.insertIntoBST(root.right, val)
return root
summary
- Experience the similarities and differences between the search process of binary search tree and ordinary binary tree .
- Experience the difference between insert and search .
边栏推荐
- Codeforces round 264 (Div. 2) C gargari and Bishop [violence]
- 注意力机制原理
- 20. (ArcGIS API for JS) ArcGIS API for JS surface collection (sketchviewmodel)
- Basic concepts of Huffman tree
- About Confidence Intervals
- Make (convert) ICO Icon
- Sorting operation partition, argpartition, sort, argsort in numpy
- Matlab Error (Matrix dimensions must agree)
- Flutter3.0了,小程序不止于移动应用跨端运行
- 我的勇敢对线之路--详细阐述,浏览器输入URL发生了什么
猜你喜欢

Search of linear table

Appx代码签名指南

概率论公式

When you go to the toilet, you can clearly explain the three Scheduling Strategies of scheduled tasks

VHDL implementation of arbitrary size matrix multiplication
![[tools] basic concept of database and MySQL installation](/img/9c/626e42097050517a13a2ce7cdab1bb.jpg)
[tools] basic concept of database and MySQL installation

R数据分析:cox模型如何做预测,高分文章复现

About Confidence Intervals

About Tolerance Intervals
![Jericho turns on the display icon of the classic Bluetooth hid mobile phone to set the keyboard [chapter]](/img/f4/8464bf9b66a1215265ac873f286688.png)
Jericho turns on the display icon of the classic Bluetooth hid mobile phone to set the keyboard [chapter]
随机推荐
树莓派设置静态ip
Create applet from 0
21.(arcgis api for js篇)arcgis api for js矩形采集(SketchViewModel)
[dream database] add the task of automatically collecting statistical information
校招行测笔试-数量关系
Jerry's broadcast has built-in flash prompt tone to control playback pause [chapter]
When you go to the toilet, you can clearly explain the three Scheduling Strategies of scheduled tasks
Not All Points Are Equal Learning Highly Efficient Point-based Detectors for 3D LiDAR Point
Cryptography series: detailed explanation of online certificate status protocol OCSP
存储过程与函数(MySQL)
Laravel php artisan 自动生成Model+Migrate+Controller 命令大全
概率论公式
Appx代码签名指南
Jericho is in non Bluetooth mode. Do not jump back to Bluetooth mode when connecting the mobile phone [chapter]
Open3d mesh filtering
Stored procedures and functions (MySQL)
Lost in the lock world of MySQL
Basic concepts of Huffman tree
Codeforces round 264 (Div. 2) C gargari and Bishop [violence]
【无标题】