class Solution:
def deleteNode(self, root, key: int) :
#辅助函数,找以root为根的最小节点(即最左)
def getMin(root):
while root.left:
root = root.left
return root
if root == None:
return
if key < root.val:
root.left = self.deleteNode(root.left, key)
elif key > root.val:
root.right = self.deleteNode(root.right, key)
else:
if root.left == None:
return root.right
elif root.right == None:
return root.left
#左右子都不为空
minNode = getMin(root.right) #找右子树中最小节点
root.val = minNode.val #将最小节点的值移到root处
root.right = self.deleteNode(root.right, minNode.val) #删除最小节点
return root