1
+ """
2
+ Find the parant and the node to be deleted. [0]
3
+ Deleting the node means replacing its reference by something else. [1]
4
+
5
+ For the node to be deleted, if it only has no child, just remove it from the parent. Return None. [2]
6
+
7
+ If it has one child, return the child. So its parent will directly connect to its child. [3]
8
+
9
+ If it has both child. Update the node's val to the minimum value in the right subtree. Remove the minimum value node in the right subtree. [4]
10
+ This is equivalent to replacing the node by the minimum value node in the right subtree.
11
+ Another option is to replace the node by the maximum value node in the left subtree.
12
+
13
+ Find the minimum value in the left subtree is easy. The leftest node value in the tree is the smallest. [5]
14
+
15
+ Time Complexity: O(LogN). O(LogN) for finding the node to be deleted.
16
+ The recursive call in `remove()` will be apply to a much smaller subtree. And much smaller subtree...
17
+ So can be ignored.
18
+ Space complexity is O(LogN). Because the recursive call will at most be called LogN times.
19
+ N is the number of nodes. And LogN can be consider the height of the tree.
20
+ """
1
21
class Solution (object ):
2
22
def deleteNode (self , root , key ):
3
23
def find_min (root ):
@@ -39,23 +59,41 @@ def remove(node):
39
59
40
60
return root
41
61
42
- """
43
- Find the parant and the node to be deleted. [0]
44
- Deleting the node means replacing its reference by something else. [1]
45
62
46
- For the node to be deleted, if it only has no child, just remove it from the parent. Return None. [2]
47
63
48
- If it has one child, return the child. So its parent will directly connect to its child. [3]
49
64
50
- If it has both child. Update the node's val to the minimum value in the right subtree. Remove the minimum value node in the right subtree. [4]
51
- This is equivalent to replacing the node by the minimum value node in the right subtree.
52
- Another option is to replace the node by the maximum value node in the left subtree.
65
+ class Solution (object ):
66
+ def deleteNode (self , node , key ):
67
+ if not node :
68
+ return node
69
+ elif key < node .val :
70
+ node .left = self .deleteNode (node .left , key )
71
+ return node
72
+ elif key > node .val :
73
+ node .right = self .deleteNode (node .right , key )
74
+ return node
75
+ elif key == node .val :
76
+ if not node .left and not node .right :
77
+ return None
78
+ elif not node .left :
79
+ return node .right
80
+ elif not node .right :
81
+ return node .left
82
+ else :
83
+ node .val = self .findMin (node .right ) #min value in the right subtree
84
+ node .right = self .deleteNode (node .right , node .val )
85
+ return node
86
+
87
+ def findMin (self , root ):
88
+ ans = float ('inf' )
89
+ stack = [root ]
90
+
91
+ while stack :
92
+ node = stack .pop ()
93
+ ans = min (ans , node .val )
94
+ if node .left : stack .append (node .left )
95
+ if node .right : stack .append (node .right )
96
+ return ans
97
+
53
98
54
- Find the minimum value in the left subtree is easy. The leftest node value in the tree is the smallest. [5]
55
99
56
- Time Complexity: O(LogN). O(LogN) for finding the node to be deleted.
57
- The recursive call in `remove()` will be apply to a much smaller subtree. And much smaller subtree...
58
- So can be ignored.
59
- Space complexity is O(LogN). Because the recursive call will at most be called LogN times.
60
- N is the number of nodes. And LogN can be consider the height of the tree.
61
- """
0 commit comments