Exemplo n.º 1
0
    def delete(self, node: TreeNode, val: int):
        if node is None:
            return

        if val < node.val:
            node.left = self.delete(node.left, val)

        if val > node.val:
            node.right = self.delete(node.right, val)

        if val == node.val:
            if node.left and node.right:
                predecessor = node.left
                while predecessor.right:
                    predecessor = predecessor.right

                node.val = predecessor.val
                predecessor.val = val
                node.left = self.delete(node.left, val)
            elif node.left:
                return node.left
            elif node.right:
                return node.right
            else:
                return None

        return node
    def delete(self, num: int, node: TreeNode = None):
        node = node or self.root

        if num < node.val:
            node.left = self.delete(num, node.left)
        elif num > node.val:
            node.right = self.delete(num, node.right)
        else:
            self.size -= 1

            if node.left is None and node.right is None:
                if self.root == node:
                    self.root = None
                return None
            elif node.left is None:
                if self.root == node:
                    self.root = node.right
                return node.right
            elif node.right is None:
                if self.root == node:
                    self.root = node.left
                return node.left
            else:
                min_node = node.right
                while min_node.left:
                    min_node = min_node.left

                node.val = min_node.val
                node.right = self.delete(min_node.val, node.right)

        return node
 def insertNode(self, node: TreeNode, val: int):
     if node.val is None or val > node.val:
         if node.right:
             self.insertNode(node.right, val)
         else:
             node.right = TreeNode(val)
     elif val < node.val:
         if node.left:
             self.insertNode(node.left, val)
         else:
             node.left = TreeNode(val)
        def helper(left: int, right: int):
            if left > right:
                return None

            mid = (left + right) // 2

            root = TreeNode(nums[mid])
            root.left = helper(left, mid - 1)
            root.right = helper(mid + 1, right)

            return root
Exemplo n.º 5
0
        def rdeserialize(l: List[str]) -> TreeNode:
            if l[0] == "None":
                l.pop(0)
                return None

            root = TreeNode(l[0])
            l.pop(0)
            root.left = rdeserialize(l)
            root.right = rdeserialize(l)

            return root
        def helper(l_idx: int, r_idx: int) -> TreeNode:
            if l_idx > r_idx:
                return None

            val = preorder.pop(0)
            node = TreeNode(val)

            idx = idx_map[val]

            node.left = helper(l_idx, idx - 1)
            node.right = helper(idx + 1, r_idx)

            return node
Exemplo n.º 7
0
    def insert(self, node: TreeNode, val: int):
        if node is None or node.val == val:
            return

        if val < node.val:
            if node.left:
                self.insert(node.left, val)
            else:
                node.left = TreeNode(val)
        if val > node.val:
            if node.right:
                self.insert(node.right, val)
            else:
                node.right = TreeNode(val)
        def helper(in_left = 0, in_right = len(inorder)) -> TreeNode:
            nonlocal pre_idx
            if in_left == in_right:
                return None

            val = preorder[pre_idx]
            node = TreeNode(val)

            pre_idx += 1

            idx = idx_map[val]
            node.left = helper(in_left, idx)
            node.right = helper(idx + 1, in_right)

            return node
        def helper(in_left: int, in_right: int) -> TreeNode:
            if in_left > in_right:
                return None

            # pick up the last element as a root
            val = postorder.pop()
            root = TreeNode(val)

            # root splits inorder list
            # into left and right subtree
            index = idx_map[val]

            # build right subtree
            root.right = helper(index + 1, in_right)
            # build left subtree
            root.left = helper(in_left, index - 1)

            return root