当前位置:网站首页>Lc669. Prune binary search tree
Lc669. Prune binary search tree
2022-07-01 22:39:00 【996 Chong Chong】
iteration
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): # Find the legal 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: # Find the unqualified one on the left
cur.left = cur.left.right # After finding it, all the left subtree can be discarded , And connect the right subtree of the unqualified node to cur On
cur = cur.left # Keep looking ( According to the characteristics of binary tree, if you don't find it, keep looking to the left )
cur = root
while cur:
while cur.right and cur.right.val > high: # Find the unqualified one on the right
cur.right = cur.right.left # After finding the right subtree, all can be discarded , And connect the left subtree of the unqualified node to cur On
cur = cur.right # Keep looking ( According to the characteristics of binary tree, if you don't find it, keep looking to the right )
return root
recursive
def trimBST(self, root, low, high): # Trim from the root node
""" :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) # Here is for pruning , And finally return the pruned result ( Is to run recursive statements first , Finally, when you return to the current statement , Will execute return)
# Equivalent to
# left = self.trimBST(root.right,low,high) # First perform recursion , The following sentence will be executed only when you return to this sentence
# return left
elif root.val > high:
return self.trimBST(root.left,low,high)
root.left = self.trimBST(root.left,low,high) # If the root node is ok , Then recursively prune the left and right child nodes
root.right = self.trimBST(root.right,low,high)
return root
recursive , Equivalent to bottom-up construction , First finish all the pruning , after return root Connect slowly , Finally, connect the pruned left and right subtrees .
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: # Prune the left and right child nodes normally
root.left = self.trimBST(root.left,low,high)
else: # If the root node is too small , And its left subtree is smaller than it, so just return to the pruning result of the right subtree
return self.trimBST(root.right,low,high)
if root.val <= high:
root.right = self.trimBST(root.right,low,high)
else: # If the root node is too large , And its right subtree is larger than it, so just return to the pruning result of the left subtree
return self.trimBST(root.left,low,high)
return root
边栏推荐
- RestTemplate 远程调用工具类
- MySQL learning notes - SQL optimization of optimization
- The difference between NiO and traditional IO
- flink sql-client 使用 对照并熟悉官方文档
- spark analyze命令使用及其作用 map join broadcast join 广播join
- 高攀不起的希尔排序,直接插入排序
- # CutefishOS系统~
- 性能测试计划怎么编写
- A debugging to understand the slot mechanism of redis cluster
- LC501. 二叉搜索树中的众数
猜你喜欢
从零开始学 MySQL —数据库和数据表操作
三翼鸟两周年:羽翼渐丰,腾飞指日可待
Dark horse programmer - software testing - stage 06 2-linux and database-01-08 Chapter 1 - description of the content of the Linux operating system stage, description of the basic format and common fo
Redis配置与优化
Delete AWS bound credit card account
内部字段分隔符
3DE resources have nothing or nothing wrong
Easyexcel complex data export
Mysql——》Innodb存储引擎的索引
keras训练的H5模型转tflite
随机推荐
信标委云原生专题组组长,任重道远!
小 P 周刊 Vol.11
91.(cesium篇)cesium火箭发射模拟
从零开始学 MySQL —数据库和数据表操作
详解Kubernetes网络模型
Unity uses SQLite
Pytorch sharpening chapter | argmax and argmin functions
Sonic cloud real machine learning summary 6 - 1.4.1 server and agent deployment
Basic knowledge of ngnix
灵动微 MM32 多路ADC-DMA配置
H5 model trained by keras to tflite
Flume interview questions
Ida dynamic debugging apk
Slope compensation
3DE 资源没东西或不对
awoo‘s Favorite Problem(优先队列)
对象内存布局
20220701
PHP reflective XSS, reflective XSS test and repair
100年仅6款产品获批,疫苗竞争背后的“佐剂”江湖