这篇文章是程序自动发表的,详情可以见
这里
href="http://ounix1xcw.bkt.clouddn.com/github.markdown.css" rel="stylesheet">
href="http://ounix1xcw.bkt.clouddn.com/github.markdown.css" rel="stylesheet">
这是leetcode的第669题--Trim a Binary Search Tree
题目 Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree. 思路
show me the code
class Solution: def trimBST(self, root, L, R): """ :type root: TreeNode :type L: int :type R: int :rtype: TreeNode """ if not root:return None if L>root.val: return self.trimBST(root.right,L,R) elif R<root.val: return self.trimBST(root.left,L,R) root.left=self.trimBST(root.left,L,R) root.right=self.trimBST(root.right,L,R) return root