mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 21:10:58 +08:00
Update 0669.修剪二叉搜索树.md
This commit is contained in:
@ -271,64 +271,72 @@ class Solution {
|
|||||||
|
|
||||||
## Python
|
## Python
|
||||||
|
|
||||||
**递归**
|
递归法(版本一)
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# Definition for a binary tree node.
|
|
||||||
# class TreeNode:
|
|
||||||
# def __init__(self, val=0, left=None, right=None):
|
|
||||||
# self.val = val
|
|
||||||
# self.left = left
|
|
||||||
# self.right = right
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode:
|
def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode:
|
||||||
'''
|
if root is None:
|
||||||
确认递归函数参数以及返回值:返回更新后剪枝后的当前root节点
|
return None
|
||||||
'''
|
|
||||||
# Base Case
|
|
||||||
if not root: return None
|
|
||||||
|
|
||||||
# 单层递归逻辑
|
|
||||||
if root.val < low:
|
if root.val < low:
|
||||||
# 若当前root节点小于左界:只考虑其右子树,用于替代更新后的其本身,抛弃其左子树整体
|
# 寻找符合区间 [low, high] 的节点
|
||||||
return self.trimBST(root.right, low, high)
|
return self.trimBST(root.right, low, high)
|
||||||
|
if root.val > high:
|
||||||
if high < root.val:
|
# 寻找符合区间 [low, high] 的节点
|
||||||
# 若当前root节点大于右界:只考虑其左子树,用于替代更新后的其本身,抛弃其右子树整体
|
|
||||||
return self.trimBST(root.left, low, high)
|
return self.trimBST(root.left, low, high)
|
||||||
|
root.left = self.trimBST(root.left, low, high) # root.left 接入符合条件的左孩子
|
||||||
|
root.right = self.trimBST(root.right, low, high) # root.right 接入符合条件的右孩子
|
||||||
|
return root
|
||||||
|
|
||||||
if low <= root.val <= high:
|
|
||||||
root.left = self.trimBST(root.left, low, high)
|
|
||||||
root.right = self.trimBST(root.right, low, high)
|
|
||||||
# 返回更新后的剪枝过的当前节点root
|
|
||||||
return root
|
|
||||||
```
|
```
|
||||||
|
递归法(版本二)精简
|
||||||
**迭代**
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
|
def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode:
|
||||||
if not root: return root
|
if root is None:
|
||||||
# 处理头结点,让root移动到[L, R] 范围内,注意是左闭右开
|
return None
|
||||||
while root and (root.val < low or root.val > high):
|
if root.val < low:
|
||||||
if root.val < low: # 小于L往右走
|
return self.trimBST(root.right, low, high)
|
||||||
root = root.right
|
if root.val > high:
|
||||||
else: # 大于R往左走
|
return self.trimBST(root.left, low, high)
|
||||||
root = root.left
|
root.left = self.trimBST(root.left, low, high)
|
||||||
# 此时root已经在[L, R] 范围内,处理左孩子元素小于L的情况
|
root.right = self.trimBST(root.right, low, high)
|
||||||
|
return root
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
迭代法
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def trimBST(self, root: TreeNode, L: int, R: int) -> TreeNode:
|
||||||
|
if not root:
|
||||||
|
return None
|
||||||
|
|
||||||
|
# 处理头结点,让root移动到[L, R] 范围内,注意是左闭右闭
|
||||||
|
while root and (root.val < L or root.val > R):
|
||||||
|
if root.val < L:
|
||||||
|
root = root.right # 小于L往右走
|
||||||
|
else:
|
||||||
|
root = root.left # 大于R往左走
|
||||||
|
|
||||||
cur = root
|
cur = root
|
||||||
|
|
||||||
|
# 此时root已经在[L, R] 范围内,处理左孩子元素小于L的情况
|
||||||
while cur:
|
while cur:
|
||||||
while cur.left and cur.left.val < low:
|
while cur.left and cur.left.val < L:
|
||||||
cur.left = cur.left.right
|
cur.left = cur.left.right
|
||||||
cur = cur.left
|
cur = cur.left
|
||||||
# 此时root已经在[L, R] 范围内,处理右孩子大于R的情况
|
|
||||||
cur = root
|
cur = root
|
||||||
|
|
||||||
|
# 此时root已经在[L, R] 范围内,处理右孩子大于R的情况
|
||||||
while cur:
|
while cur:
|
||||||
while cur.right and cur.right.val > high:
|
while cur.right and cur.right.val > R:
|
||||||
cur.right = cur.right.left
|
cur.right = cur.right.left
|
||||||
cur = cur.right
|
cur = cur.right
|
||||||
|
|
||||||
return root
|
return root
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Go
|
## Go
|
||||||
|
Reference in New Issue
Block a user