Beispiel #1
0
 def invertTree(self, root: TreeNode) -> TreeNode:
     if not root:
         return root
     left = self.invertTree(root.left)
     right = self.invertTree(root.right)
     root.left, root.right = right, left
     return root
Beispiel #2
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 #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 helper(p: TreeNode):
     nonlocal pre
     if not p:
         return p
     helper(p.right)
     helper(p.left)
     p.left, p.right = None, pre
     pre = p
Beispiel #5
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 #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
Beispiel #7
0
 def helper(p: TreeNode) -> TreeNode:
     if not p or (not p.left and not p.right):
         return p
     right = p.right
     ltail = helper(p.left)
     if ltail:
         p.left, p.right = None, p.left
         ltail.right = right
     return helper(right) if right else ltail
 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)
 def connect(self, h1: TreeNode, h2: TreeNode) -> TreeNode:
     if not h1:
         return h2
     if not h2:
         return h1
     t1, t2 = h1.left, h2.left
     h2.left, t1.right = t1, h2
     t2.right, h1.left = h1, t2
     return h1
Beispiel #10
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 #11
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 #12
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 #13
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 #14
0
 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