当前位置:网站首页>LC669. 修剪二叉搜索树
LC669. 修剪二叉搜索树
2022-07-01 21:45:00 【996冲冲冲】
迭代
def trimBST(self, root, low, high):
""" :type root: TreeNode :type low: int :type high: int :rtype: TreeNode """
if not root:
return
while root and (root.val < low or root.val > high): #找到合法的root
if root.val < low:
root = root.right
elif root.val > high:
root = root.left
cur = root
while cur:
while cur.left and cur.left.val < low: #找左边不合格的
cur.left = cur.left.right #找到之后左子树所有的都可以丢掉,并且把不合格节点的右子树连接到cur上
cur = cur.left #接着找(根据二叉树的特性没找到就一直往左找)
cur = root
while cur:
while cur.right and cur.right.val > high: #找右边不合格的
cur.right = cur.right.left #找到后右子树的所有都可以丢掉,并且把不合格节点的左子树连接到cur上
cur = cur.right #接着找(根据二叉树的特性没找到就一直往右找)
return root
递归
def trimBST(self, root, low, high): #从根节点开始修剪
""" :type root: TreeNode :type low: int :type high: int :rtype: TreeNode """
if not root:
return
if root.val < low:
return self.trimBST(root.right,low,high) #这里是进行修剪,并且最后返回修剪之后的结果(是先运行递归的语句,最后返回到当前语句时,才会执行return)
# 等价于
# left = self.trimBST(root.right,low,high) #先执行递归,返回到这一句时才会执行下面的那句
# return left
elif root.val > high:
return self.trimBST(root.left,low,high)
root.left = self.trimBST(root.left,low,high) #如果根节点没问题,则递归地修剪左子结点和右子结点
root.right = self.trimBST(root.right,low,high)
return root
递归,相当于自底向上的构建,先把所有的修剪完毕,之后return root时慢慢连接,最后把修剪好的左右子树连接上。
def trimBST(self, root, low, high):
""" :type root: TreeNode :type low: int :type high: int :rtype: TreeNode """
if not root:
return
if root.val >= low: #正常就修剪左子节点和右子节点
root.left = self.trimBST(root.left,low,high)
else: #如果根节点太小,且他的左子树都小于它所以直接返回右子树的修剪结果就行
return self.trimBST(root.right,low,high)
if root.val <= high:
root.right = self.trimBST(root.right,low,high)
else: #如果根节点太大,且他的右子树都大于它所以直接返回左子树的修剪结果就行
return self.trimBST(root.left,low,high)
return root
边栏推荐
- One of the basic learning of function
- #yyds干货盘点# 解决名企真题:扭蛋机
- Count the number of each character in the character
- pytest合集(2)— pytest運行方式
- [STM32] stm32cubemx tutorial II - basic use (new projects light up LED lights)
- “丝路正青春 风采看福建”在闽外籍青年短视频大赛火热征集作品中
- 快乐数[环类问题之快慢指针]
- Four methods of JS array splicing [easy to understand]
- Pytest Collection (2) - mode de fonctionnement pytest
- The difference between NiO and traditional IO
猜你喜欢

CIO's discussion and Analysis on the definition of high-performance it team
![[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!](/img/27/4bd0de716f5cb360d54f140dc8e9c1.png)
[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!

Basic operation of binary tree
![比较版本号[双指针截取自己想要的字串]](/img/19/4f858ffdc1281d6b8b18a996467f10.png)
比较版本号[双指针截取自己想要的字串]

AIDL基本使用

【目标跟踪】|单目标跟踪指标

Spark interview questions

pytest合集(2)— pytest運行方式

GenICam GenTL 标准 ver1.5(4)第五章 采集引擎

Why does blocprovider feel similar to provider?
随机推荐
【MySQL】索引的创建、查看和删除
Go — 相关依赖对应的exe
CIO's discussion and Analysis on the definition of high-performance it team
Microsoft, Columbia University | Godel: large scale pre training of goal oriented dialogue
MySQL empties table data
使用闭包实现点击按钮切换 toggle
Basic knowledge of ngnix
Is PMP certificate really useful?
Difference and use between require and import
Go - exe corresponding to related dependency
[monomer] recommended configuration of streaming information i-bpsv3 server
指标陷阱:IT领导者易犯的七个KPI错误
Four methods of JS array splicing [easy to understand]
[ecological partner] Kunpeng system engineer training
PyTorch磨刀篇|argmax和argmin函数
13th Blue Bridge Cup group B national tournament
Business visualization - make your flowchart'run'up
ICML2022 | 基于元语义正则化的介入性对比学习
Use of vscode
Burpsuite simple packet capturing tutorial [easy to understand]