当前位置:网站首页>[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 .
边栏推荐
- 迷失在MySQL的锁世界
- Not All Points Are Equal Learning Highly Efficient Point-based Detectors for 3D LiDAR Point
- 小程序能运行在自有App中,且实现直播和连麦?
- Restcloud ETL Community Edition June featured Q & A
- Significance and measures of source code confidentiality
- HDU 4337 King Arthur' S Knights it outputs a Hamiltonian circuit
- 22. (ArcGIS API for JS) ArcGIS API for JS Circle Collection (sketchviewmodel)
- Shell programming basics
- Sub pixel corner detection opencv cornersubpix
- About Estimation Statistics
猜你喜欢

Ubuntu 20 installation des enregistrements redisjson

概率论公式
![Jerry's broadcast has built-in flash prompt tone to control playback pause [chapter]](/img/8c/e8f7e667e4762a4815e97c36a2759f.png)
Jerry's broadcast has built-in flash prompt tone to control playback pause [chapter]

Sub pixel corner detection opencv cornersubpix

体会设计细节

「小样本深度学习图像识别」最新2022综述

Significance and measures of source code confidentiality

亚像素级角点检测Opencv-cornerSubPix

VHDL implementation of arbitrary size matrix multiplication

Depth analysis of compilation constants, classloader classes, and system class loaders
随机推荐
Intelligent static presence detection scheme, 5.8G radar sensing technology, human presence inductive radar application
安装 torch 0.4.1
Shangsilicon Valley JVM Chapter 1 class loading subsystem
编译常量、ClassLoader类、系统类加载器深度探析
Vernacular high concurrency (2)
The latest 2022 review of "small sample deep learning image recognition"
Flink Task退出流程与Failover机制
Function reentry, function overloading and function rewriting are understood by yourself
U.S. Air Force Research Laboratory, "exploring the vulnerability and robustness of deep learning systems", the latest 85 page technical report in 2022
VHDL实现任意大小矩阵加法运算
Domcontentloaded and window onload
腾讯云原生数据库TDSQL-C入选信通院《云原生产品目录》
Opencv environment, and open a local PC camera.
19. (ArcGIS API for JS) ArcGIS API for JS line acquisition (sketchviewmodel)
制作(转换)ico图标
21. (article ArcGIS API for JS) ArcGIS API for JS rectangular acquisition (sketchviewmodel)
Enumeration general interface & enumeration usage specification
R data analysis: how to predict Cox model and reproduce high score articles
Laravel php artisan 自动生成Model+Migrate+Controller 命令大全
23. (ArcGIS API for JS) ArcGIS API for JS ellipse collection (sketchviewmodel)