题目:
题解:
java
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
TreeNode cur = root, curParent = null;
while (cur != null && cur.val != key) {
curParent = cur;
if (cur.val > key) {
cur = cur.left;
} else {
cur = cur.right;
}
}
if (cur == null) {
return root;
}
if (cur.left == null && cur.right == null) {
cur = null;
} else if (cur.right == null) {
cur = cur.left;
} else if (cur.left == null) {
cur = cur.right;
} else {
TreeNode successor = cur.right, successorParent = cur;
while (successor.left != null) {
successorParent = successor;
successor = successor.left;
}
if (successorParent.val == cur.val) {
successorParent.right = successor.right;
} else {
successorParent.left = successor.right;
}
successor.right = cur.right;
successor.left = cur.left;
cur = successor;
}
if (curParent == null) {
return cur;
} else {
if (curParent.left != null && curParent.left.val == key) {
curParent.left = cur;
} else {
curParent.right = cur;
}
return root;
}
}
}