Skip to content

Commit db29574

Browse files
authored
添加 0669.修剪二叉搜索树 python3版本
添加 0669.修剪二叉搜索树 python3版本
1 parent fa76dfe commit db29574

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

problems/0669.修剪二叉搜索树.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,24 @@ class Solution {
265265
```
266266

267267
Python:
268-
269-
268+
```python3
269+
# Definition for a binary tree node.
270+
# class TreeNode:
271+
# def __init__(self, val=0, left=None, right=None):
272+
# self.val = val
273+
# self.left = left
274+
# self.right = right
275+
class Solution:
276+
def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode:
277+
if not root: return root
278+
if root.val < low:
279+
return self.trimBST(root.right,low,high) // 寻找符合区间[low, high]的节点
280+
if root.val > high:
281+
return self.trimBST(root.left,low,high) // 寻找符合区间[low, high]的节点
282+
root.left = self.trimBST(root.left,low,high) // root->left接入符合条件的左孩子
283+
root.right = self.trimBST(root.right,low,high) // root->right接入符合条件的右孩子
284+
return root
285+
```
270286
Go:
271287

272288

@@ -276,4 +292,4 @@ Go:
276292
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
277293
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
278294
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
279-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
295+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

0 commit comments

Comments
 (0)