给定一个二叉搜索树,同时给定最小边界L 和最大边界 R。通过修剪二叉搜索树,使得所有节点的值在[L, R]中 (R>=L) 。你可能需要改变树的根节点,所以结果应当返回修剪好的二叉搜索树的新的根节点。
示例 1:
输入:
1
/ \
0 2
L = 1
R = 2
输出:
1
\
2
示例 2:
输入:
3
/ \
0 4
\
2
/
1
L = 1
R = 3
输出:
3
/
2
/
1
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/trim-a-binary-search-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def trimBST(self, root, L, R):
"""
:type root: TreeNode
:type L: int
:type R: int
:rtype: TreeNode
"""
if root is None:
return None
if root.val>R:
return self.trimBST(root.left,L,R)
if root.val<L:
return self.trimBST(root.right,L,R)
if root.val>=L and root.val<=R:
root.left=self.trimBST(root.left,L,R)
root.right=self.trimBST(root.right,L,R)
return root
执行结果:
通过
显示详情
执行用时:40 ms, 在所有 Python 提交中击败了79.03%的用户
内存消耗:20.5 MB, 在所有 Python 提交中击败了100.00%的用户