当前位置:网站首页>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
边栏推荐
- Is PMP certificate really useful?
- 【目标跟踪】|单目标跟踪指标
- In the past 100 years, only 6 products have been approved, which is the "adjuvant" behind the vaccine competition
- 园区全光技术选型-中篇
- Design and practice of new generation cloud native database
- GaussDB(DWS)主动预防排查
- Smart micro mm32 multi-channel adc-dma configuration
- How to write a performance test plan
- 切面条 C语言
- 91.(cesium篇)cesium火箭发射模拟
猜你喜欢
随机推荐
20220701
[intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial
Learn MySQL from scratch - database and data table operations
H5 model trained by keras to tflite
Relationship and difference between enterprise architecture and project management
详解ThreadLocal
Spark interview questions
Easyexcel complex data export
Training on the device with MIT | 256Kb memory
Smart micro mm32 multi-channel adc-dma configuration
MySQL learning notes - SQL optimization of optimization
Count the number of each character in the character
Redis配置与优化
Burpsuite simple packet capturing tutorial [easy to understand]
3DE 资源没东西或不对
Indicator trap: seven KPI mistakes that it leaders are prone to make
MySQL之MHA高可用配置及故障切换
快乐数[环类问题之快慢指针]
Icml2022 | interventional contrastive learning based on meta semantic regularization
91.(cesium篇)cesium火箭发射模拟








