https://leetcode.com/problems/delete-node-in-a-bst



Success



```python
class Solution:
    def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
        #2023/1/26 06:23
        if root == None:
            return root

        self.ok = False
        self.parent_node = None
        self.direction = None
        self.node = None
        def travel(node, parent, direction):
            if self.ok == True:
                return
            if node:
                value = node.val
                if value == key:
                    self.parent_node = parent
                    self.direction = direction
                    self.node = node
                        
                    self.ok = True
                travel(node.left, node, -1)
                travel(node.right, node, 1)

        if root.val == key:
            self.node = root
        else:
            travel(root, None, 1)

        def get_subnode_values(node):
            if node:
                return [node.val] + get_subnode_values(node.left) + get_subnode_values(node.right)
            else:
                return [None]
        if self.node != None:
            values = get_subnode_values(self.node.left)
            values += get_subnode_values(self.node.right)
            values = [value for value in values if value != None and value != key]
            values.sort()
            #print(values)
    
            def sortedArrayToBSTRecu(num_list, start, end):
                if start == end:
                    return None
                mid = start + (end - start) // 2
                node = TreeNode(num_list[mid])
                node.left = sortedArrayToBSTRecu(num_list, start, mid)
                node.right = sortedArrayToBSTRecu(num_list, mid + 1, end)
                return node
            new_sub_root = sortedArrayToBSTRecu(values, 0, len(values))
    
            if self.parent_node != None:
                if self.direction == -1:
                    self.parent_node.left = new_sub_root
                elif self.direction == 1:
                    self.parent_node.right = new_sub_root
            else:
                return new_sub_root
        else:
            pass

        return root
        #2023/1/26 07:03
```
