Merge pull request #223 from jojoo15/patch-17

添加 0669.修剪二叉搜索树 python3版本
This commit is contained in:
Carl Sun
2021-05-22 00:26:34 +08:00
committed by GitHub

View File

@ -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