File tree Expand file tree Collapse file tree 1 file changed +19
-3
lines changed Expand file tree Collapse file tree 1 file changed +19
-3
lines changed Original file line number Diff line number Diff line change @@ -265,8 +265,24 @@ class Solution {
265
265
```
266
266
267
267
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
+ ```
270
286
Go:
271
287
272
288
276
292
* 作者微信:[ 程序员Carl] ( https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw )
277
293
* B站视频:[ 代码随想录] ( https://space.bilibili.com/525438321 )
278
294
* 知识星球:[ 代码随想录] ( 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 >
You can’t perform that action at this time.
0 commit comments