Beispiel #1
0
 def increasingBST(self, root: TreeNode, nxt: TreeNode = None) -> TreeNode:
     if not root:
         return nxt
     head = self.increasingBST(root.left, root)
     root.left = None
     root.right = self.increasingBST(root.right, nxt)
     return head
Beispiel #2
0
 def deleteNode(self, root: TreeNode, key: int) -> TreeNode:
     if root is None:
         return root
     if key < root.val:
         root.left = self.deleteNode(root.left, key)
     elif key > root.val:
         root.right = self.deleteNode(root.right, key)
     else:  # found the key
         if root.left is None:
             return root.right
         if root.right is None:
             return root.left
         min_val = self.findMin(root.right)
         root.val = min_val
         root.right = self.deleteNode(root.right, min_val)
     return root
Beispiel #3
0
 def build(xs: List[int], ubound: int) -> Optional[TreeNode]:
     if not xs or xs[-1] > ubound:
         return None
     root = TreeNode(xs.pop())
     root.left = build(xs, root.val)
     root.right = build(xs, ubound)
     return root
Beispiel #4
0
 def pruneTreeV2(self, root: TreeNode) -> TreeNode:
     if not root:
         return None
     root.left = self.pruneTree(root.left)
     root.right = self.pruneTree(root.right)
     if not root.left and not root.right and not root.val:
         return None
     return root
Beispiel #5
0
 def pruneTreeV1(self, root: TreeNode) -> TreeNode:
     def not_contain_1(root: TreeNode) -> bool:
         if not root:
             return True
         if left_not_contain_1 := not_contain_1(root.left):
             root.left = None
         if right_not_contain_1 := not_contain_1(root.right):
             root.right = None
Beispiel #6
0
 def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
     if not root:
         return TreeNode(val)
     if val < root.val:
         root.left = self.insertIntoBST(root.left, val)
     else:
         root.right = self.insertIntoBST(root.right, val)
     return root
 def treeToDoublyList(self, root: TreeNode) -> TreeNode:
     if not root:
         return None
     lhead = self.treeToDoublyList(root.left)
     rhead = self.treeToDoublyList(root.right)
     # make root itself a single node DLL
     root.left = root
     root.right = root
     return self.connect(self.connect(lhead, root), rhead)
Beispiel #8
0
 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)
     elif root.val > high:
         return self.trimBST(root.left, low, high)
     else:
         root.left = self.trimBST(root.left, low, root.val)
         root.right = self.trimBST(root.right, root.val, high)
         return root
Beispiel #9
0
 def _deserialize(buffer: List[str], lower: int,
                  upper: int) -> "TreeNode":
     if len(buffer) == 0:
         return None
     if buffer[0] < lower or buffer[0] > upper:
         return None
     val = buffer.pop(0)
     root = TreeNode(val)
     root.left = _deserialize(buffer, lower, val)
     root.right = _deserialize(buffer, val, upper)
     return root
Beispiel #10
0
 def addOneRow(self,
               root: TreeNode,
               v: int,
               d: int,
               left: bool = True) -> TreeNode:
     if d == 1:
         nroot = TreeNode(v, root if left else None, None if left else root)
         return nroot
     if not root:
         return root
     root.left = self.addOneRow(root.left, v, d - 1, True)
     root.right = self.addOneRow(root.right, v, d - 1, False)
     return root
Beispiel #11
0
 def helper(node: TreeNode) -> Tuple[TreeNode, TreeNode]:
     if not node:
         return None, None
     if node.left:
         head, ltail = helper(node.left)
         ltail.right, node.left = node, None
     else:
         head = node
     if node.right:
         node.right, rtail = helper(node.right)
     else:
         rtail = node
     return head, rtail