-
Notifications
You must be signed in to change notification settings - Fork 17
/
delete-node-in-a-bst.py
57 lines (47 loc) · 1.69 KB
/
delete-node-in-a-bst.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from typing import Tuple
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def deleteNode(self, root: TreeNode, key: int) -> TreeNode:
def find(root: TreeNode, key: int) -> Tuple[TreeNode, TreeNode]:
node = root
parent = None
while node:
if node.val < key:
parent = node
node = node.right
elif node.val > key:
parent = node
node = node.left
else:
return parent, node
return parent, None
def assign_node(parent: TreeNode, node: Optional[TreeNode], val: int) -> None:
if parent.val < val:
parent.right = node
elif parent.val > val:
parent.left = node
else:
raise RuntimeError("Expect unique values in the tree")
def insert(root: TreeNode, node: TreeNode) -> None:
parent, _ = find(root, node.val)
assign_node(parent, node, node.val)
parent, node = find(root, key)
if not node:
return root
if node.left and node.right:
insert(node.left, node.right)
if parent:
if node.left:
assign_node(parent, node.left, node.val)
elif node.right:
assign_node(parent, node.right, node.val)
else:
assign_node(parent, None, node.val)
else:
root = node.left or node.right
return root