mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge pull request #223 from jojoo15/patch-17
添加 0669.修剪二叉搜索树 python3版本
This commit is contained in:
@ -265,8 +265,24 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
||||
```python3
|
||||
# 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:
|
||||
if not root: return root
|
||||
if root.val < low:
|
||||
return self.trimBST(root.right,low,high) // 寻找符合区间[low, high]的节点
|
||||
if root.val > high:
|
||||
return self.trimBST(root.left,low,high) // 寻找符合区间[low, high]的节点
|
||||
root.left = self.trimBST(root.left,low,high) // root->left接入符合条件的左孩子
|
||||
root.right = self.trimBST(root.right,low,high) // root->right接入符合条件的右孩子
|
||||
return root
|
||||
```
|
||||
Go:
|
||||
|
||||
|
||||
@ -276,4 +292,4 @@ Go:
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
|
Reference in New Issue
Block a user