Example:
root = [5,3,6,2,4,null,7] key = 3 5 / \ 3 6 / \ \ 2 4 7 Given that the value of the node to be deleted is 3, we first find the 3 node and then delete it. The correct answer is [5,4,6,2,null,null,7], as shown in the figure below. 5 / \ 4 6 / \ 2 7 Another correct answer is [5,2,6,null,4,null,7]. 5 / \ 2 6 \ \ 4 7
Idea analysis: This problem is not difficult, but requires a clear mind.
| If the node to be deleted is the root of the current tree, || If root - > right = NULL, the result returned is root - > left || Otherwise, we need to find the smallest node tempPtr from root - > right, cut it off from root - > right, and then put root - > left in tempPtr - > left, root - > right in tempPtr - > right. | Otherwise, we use the nature of the binary search tree to find the location of the node that needs to be deleted and its parent node, call ourselves recursively, delete that node, return to the spliced subtree, and put it back to the corresponding location of the parent node.
For example, in the processing of the example, if root - > Val = Val is not equal to root, then root is changed to the corresponding location of key and beforeDel is changed to the parent node of the corresponding node of key.
Then call yourself, the current root (left node 3 of 5) is the need to delete the node, key (left node 5) before deleting, modify the current root of the left and right.
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: //Find the smallest node in the root tree, intercept it from the tree, and place the root on the right of the smallest node TreeNode* findMin(TreeNode *root){ TreeNode *res, *start = root; //The special case is root itself. if (root->left == NULL){ return root; } //Otherwise, keep looking for left while (root->left->left){ root = root->left; } //Cut this node off the tree res = root->left; root->left = root->left->right; //Place the original root on the right side of the node res->right = start; return res; } TreeNode* deleteNode(TreeNode* root, int key) { if (root == NULL){ return NULL; } //Deal with special cases, delete root TreeNode *result = root; if (root->val == key){ if (root->right == NULL){ //Root - > right is empty, and the result is root - > left result = root->left; } else{ //Modify root - > right (place root - > right under the smallest child node in root - > right, and put back the smallest child node) TreeNode *tempPtr = findMin(root->right); result = tempPtr; tempPtr->left = root->left;//Place root - > left in tempPtr } delete root; return result; } //Locate the node that needs to be deleted TreeNode *beforeDel = root;//Need to delete the parent node of the node while (root != NULL && root->val != key){ beforeDel = root; //Properties of Binary Search Tree: Left Subtree < Root < Right Subtree if (root->val > key){ root = root->left; } else{ root = root->right; } } //Call yourself recursively and delete the current root if (beforeDel->left == root){ beforeDel->left = deleteNode(root, key); } else{ beforeDel->right = deleteNode(root, key); } return result; } };