Example #1
0
 def create_bst_part(self, items: [], start, end):
     # Parent node will be a median of items
     im = math.floor(start + (end - start) / 2)
     node = TreeNode(items[im], None, None)
     # Recursive call to fill left/right children of BST
     if im > start:
         node.left = self.create_bst_part(items, start, im)
     if end > im + 1:
         node.right = self.create_bst_part(items, im, end)
     return node
Example #2
0
 def trimBST(self, root: TreeNode, L: int, R: int) -> TreeNode:
     if not root:
         return None
     if root.val < L:
         root = self.trimBST(root.right, L, R)
     elif root.val > R:
         root = self.trimBST(root.left, L, R)
     else:
         root.left = self.trimBST(root.left, L, R)
         root.right = self.trimBST(root.right, L, R)
     return root