当前位置:网站首页>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
边栏推荐
- Go - exe corresponding to related dependency
- Wechat applet, continuously playing multiple videos. Synthesize the appearance of a video and customize the video progress bar
- 并发编程系列之FutureTask源码学习笔记
- #yyds干货盘点# 解决名企真题:扭蛋机
- linux下清理系统缓存并释放内存
- MIT|256KB 内存下的设备上训练
- CIO's discussion and Analysis on the definition of high-performance it team
- 基于三维GIS的不动产管理应用
- Copy ‘XXXX‘ to effectively final temp variable
- Internet of things RFID, etc
猜你喜欢
Basic knowledge of ngnix
Microsoft, Columbia University | Godel: large scale pre training of goal oriented dialogue
In the past 100 years, only 6 products have been approved, which is the "adjuvant" behind the vaccine competition
mysql 学习笔记-优化之SQL优化
GenICam GenTL 标准 ver1.5(4)第五章 采集引擎
Mysql——》MyISAM存储引擎的索引
[deep learning] use deep learning to monitor your girlfriend's wechat chat?
企业架构与项目管理的关联和区别
比较版本号[双指针截取自己想要的字串]
Spark interview questions
随机推荐
Which securities company should we choose to open an account for flush stock? Is it safe to open an account with a mobile phone?
【MySQL】索引的分类
黑马程序员-软件测试--06阶段2-linux和数据库-01-08第一章-linux操作系统阶段内容说明,linux命令基本格式以及常见形式的说明,操作系统的常见的分类,查看命令帮助信息方法,
What is the difference between PMP and NPDP?
[deep learning] use deep learning to monitor your girlfriend's wechat chat?
并发编程系列之FutureTask源码学习笔记
详解Volatile关键字
What is the difference between consonants and Initials? (difference between initials and consonants)
Qtreeview+qabstractitemmodel custom model: the third of a series of tutorials [easy to understand]
基于三维GIS的不动产管理应用
高攀不起的希尔排序,直接插入排序
13th Blue Bridge Cup group B national tournament
mysql 学习笔记-优化之SQL优化
Show member variables and methods in classes in idea
The difference between NiO and traditional IO
收到一封CTO来信,邀约面试机器学习工程师
Icml2022 | interventional contrastive learning based on meta semantic regularization
辅音和声母的区别?(声母与辅音的区别)
Relationship and difference between enterprise architecture and project management
[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!