题目:
题解:
func deleteNode(root *TreeNode, key int) *TreeNode {var cur, curParent *TreeNode = root, nilfor cur != nil && cur.Val != key {curParent = curif cur.Val > key {cur = cur.Left} else {cur = cur.Right}}if cur == nil {return root}if cur.Left == nil && cur.Right == nil {cur = nil} else if cur.Right == nil {cur = cur.Left} else if cur.Left == nil {cur = cur.Right} else {successor, successorParent := cur.Right, curfor successor.Left != nil {successorParent = successorsuccessor = successor.Left}if successorParent.Val == cur.Val {successorParent.Right = successor.Right} else {successorParent.Left = successor.Right}successor.Right = cur.Rightsuccessor.Left = cur.Leftcur = successor}if curParent == nil {return cur}if curParent.Left != nil && curParent.Left.Val == key {curParent.Left = cur} else {curParent.Right = cur}return root
}