题目:
题解:
class Solution:def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:cur, curParent = root, Nonewhile cur and cur.val != key:curParent = curcur = cur.left if cur.val > key else cur.rightif cur is None:return rootif cur.left is None and cur.right is None:cur = Noneelif cur.right is None:cur = cur.leftelif cur.left is None:cur = cur.rightelse:successor, successorParent = cur.right, curwhile successor.left:successorParent = successorsuccessor = successor.leftif successorParent.val == cur.val:successorParent.right = successor.rightelse:successorParent.left = successor.rightsuccessor.right = cur.rightsuccessor.left = cur.leftcur = successorif curParent is None:return curif curParent.left and curParent.left.val == key:curParent.left = curelse:curParent.right = curreturn root