当前位置:网站首页>[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 .
边栏推荐
- 树莓派设置静态ip
- 变量、流程控制与游标(MySQL)
- HDU 4337 King Arthur' S Knights it outputs a Hamiltonian circuit
- 【colmap】已知相机位姿情况下进行三维重建
- HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
- Numpy中排序操作partition,argpartition,sort,argsort
- Mathematical induction and recursion
- Depth analysis of compilation constants, classloader classes, and system class loaders
- Set WiFi automatic connection for raspberry pie
- Make (convert) ICO Icon
猜你喜欢

Graphical tools package yolov5 and generate executable files exe

leetcode

图形化工具打包YOLOv5,生成可执行文件EXE

树莓派设置wifi自动连接

CVPR 2022 best paper candidate | pip: six inertial sensors realize whole body dynamic capture and force estimation

Lavel PHP artisan automatically generates a complete set of model+migrate+controller commands

21.(arcgis api for js篇)arcgis api for js矩形采集(SketchViewModel)

小程序能运行在自有App中,且实现直播和连麦?

亚像素级角点检测Opencv-cornerSubPix

哈夫曼树基本概念
随机推荐
如何自定义Latex停止运行的快捷键
【安全的办公和生产力应用程序】上海道宁为您提供ONLYOFFICE下载、试用、教程
Restcloud ETL Community Edition June featured Q & A
Under the tide of "going from virtual to real", Baidu AI Cloud is born from real
Flutter3.0了,小程序不止于移动应用跨端运行
迷失在MySQL的锁世界
[colmap] 3D reconstruction with known camera pose
大白话高并发(二)
“去虚向实”大潮下,百度智能云向实而生
海思万能平台搭建:颜色空间转换YUV2RGB
Appx代码签名指南
19.(arcgis api for js篇)arcgis api for js线采集(SketchViewModel)
线性表的查找
Flink task exit process and failover mechanism
枚举通用接口&枚举使用规范
Jerry's phonebook acquisition [chapter]
Graphical tools package yolov5 and generate executable files exe
22. (ArcGIS API for JS) ArcGIS API for JS Circle Collection (sketchviewmodel)
How to customize the shortcut key for latex to stop running
PIP download only, not install