Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 41 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
May 27, 2023
Merged
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
4b575a8
Update 0450.删除二叉搜索树中的节点.md
fwqaaq Dec 7, 2022
d97f276
Update problems/0450.删除二叉搜索树中的节点.md
fwqaaq Dec 9, 2022
94f79ad
Update 0001.两数之和.md
coffeelize Mar 19, 2023
633cb54
为0226.翻转二叉树添加了C#语言版(包含递归和迭代)
JavieDeng Mar 20, 2023
da2acd7
"为0226.翻转二叉树添加了C#语言版(包含递归和迭代)"
JavieDeng Mar 20, 2023
0231e5f
Merge branch 'youngyangyang04:master' into master
JavieDeng Mar 22, 2023
f51f312
Update 0406.根据身高重建队列.md
fwqaaq Mar 23, 2023
b934f47
Update 0647.回文子串.md
milu-tao Mar 24, 2023
40f8230
Update 0106.从中序与后序遍历序列构造二叉树.md
jianghongcheng May 9, 2023
7eea41e
Update 0654.最大二叉树.md
jianghongcheng May 9, 2023
a45046b
Update 0617.合并二叉树.md
jianghongcheng May 9, 2023
3818de4
Update 0700.二叉搜索树中的搜索.md
jianghongcheng May 9, 2023
f5329cd
Update 0110.平衡二叉树.md
jianghongcheng May 22, 2023
2024fc2
Update 0257.二叉树的所有路径.md
jianghongcheng May 22, 2023
9b770de
Update 0257.二叉树的所有路径.md
jianghongcheng May 22, 2023
1a67274
Update 0257.二叉树的所有路径.md
jianghongcheng May 22, 2023
2e81b18
Update 0257.二叉树的所有路径.md
jianghongcheng May 23, 2023
117ef69
Update 0404.左叶子之和.md
jianghongcheng May 23, 2023
1ac1a8c
Update 0513.找树左下角的值.md
jianghongcheng May 23, 2023
12634b2
Update 0654.最大二叉树.md
jianghongcheng May 23, 2023
41cf3a4
Update 0654.最大二叉树.md
jianghongcheng May 23, 2023
7a544d9
Update 0654.最大二叉树.md
jianghongcheng May 23, 2023
62d48cf
Merge pull request #2083 from jianghongcheng/master
youngyangyang04 May 23, 2023
3f4968a
Merge pull request #1978 from milu-tao/master
youngyangyang04 May 24, 2023
2b2fd97
Merge pull request #1976 from fwqaaq/patch-23
youngyangyang04 May 24, 2023
442349a
Merge pull request #1968 from JavieDeng/master
youngyangyang04 May 24, 2023
58fb2cb
Merge pull request #1807 from fwqaaq/patch-11
youngyangyang04 May 24, 2023
3058654
Update 0098.验证二叉搜索树.md
jianghongcheng May 24, 2023
85e4c41
Update 0530.二叉搜索树的最小绝对差.md
jianghongcheng May 24, 2023
c5d1845
Update 0501.二叉搜索树中的众数.md
jianghongcheng May 24, 2023
daa5417
Update 0236.二叉树的最近公共祖先.md
jianghongcheng May 24, 2023
e7b7aa8
Update 0235.二叉搜索树的最近公共祖先.md
jianghongcheng May 24, 2023
511bf44
Update 0701.二叉搜索树中的插入操作.md
jianghongcheng May 24, 2023
731d1ca
Update 0450.删除二叉搜索树中的节点.md
jianghongcheng May 24, 2023
5365274
Update 0669.修剪二叉搜索树.md
jianghongcheng May 24, 2023
76e3811
Update 0108.将有序数组转换为二叉搜索树.md
jianghongcheng May 24, 2023
14acf5a
Update 0538.把二叉搜索树转换为累加树.md
jianghongcheng May 24, 2023
450b3f2
Update 0111.二叉树的最小深度.md
jianghongcheng May 24, 2023
832897e
Update 0110.平衡二叉树.md
jianghongcheng May 24, 2023
d420634
Merge pull request #2096 from jianghongcheng/master
youngyangyang04 May 25, 2023
44477f8
Merge pull request #1962 from coffeelize/patch-1
youngyangyang04 May 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update 0669.修剪二叉搜索树.md
  • Loading branch information
jianghongcheng authored May 24, 2023
commit 53652744d2aa05765a10ce824424ee1c06afc57c
82 changes: 45 additions & 37 deletions problems/0669.修剪二叉搜索树.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -271,64 +271,72 @@ class Solution {

## 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:
def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode:
'''
确认递归函数参数以及返回值:返回更新后剪枝后的当前root节点
'''
# Base Case
if not root: return None

# 单层递归逻辑
if root is None:
return None
if root.val < low:
# 若当前root节点小于左界:只考虑其右子树,用于替代更新后的其本身,抛弃其左子树整体
# 寻找符合区间 [low, high] 的节点
return self.trimBST(root.right, low, high)

if high < root.val:
# 若当前root节点大于右界:只考虑其左子树,用于替代更新后的其本身,抛弃其右子树整体
if root.val > high:
# 寻找符合区间 [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
class Solution:
def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode:
if root is None:
return None
if root.val < low:
return self.trimBST(root.right, low, high)
if 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

**迭代**

```

迭代法
```python
class Solution:
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
if not root: return root
# 处理头结点,让root移动到[L, R] 范围内,注意是左闭右开
while root and (root.val < low or root.val > high):
if root.val < low: # 小于L往右走
root = root.right
else: # 大于R往左走
root = root.left
# 此时root已经在[L, R] 范围内,处理左孩子元素小于L的情况
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

# 此时root已经在[L, R] 范围内,处理左孩子元素小于L的情况
while cur:
while cur.left and cur.left.val < low:
while cur.left and cur.left.val < L:
cur.left = cur.left.right
cur = cur.left
# 此时root已经在[L, R] 范围内,处理右孩子大于R的情况

cur = root

# 此时root已经在[L, R] 范围内,处理右孩子大于R的情况
while cur:
while cur.right and cur.right.val > high:
while cur.right and cur.right.val > R:
cur.right = cur.right.left
cur = cur.right

return root

```

## Go
Expand Down

AltStyle によって変換されたページ (->オリジナル) /