mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
Merge pull request #223 from jojoo15/patch-17
添加 0669.修剪二叉搜索树 python3版本
This commit is contained in:
@ -265,8 +265,24 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
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:
|
Go:
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user