Complete the code to check if the current node is null before deletion.
if (root == [1]) { return root; }
In C++, the correct way to check for a null pointer is nullptr.
Complete the code to find the minimum value node in the right subtree.
Node* minValueNode(Node* node) {
Node* current = node;
while (current->[1] != nullptr) {
current = current->left;
}
return current;
}To find the minimum value node, we keep going left until we reach a null pointer.
Fix the error in the recursive delete call for the right subtree.
root->right = deleteNode(root->right, [1]);The key to delete is passed down recursively, not the current node's value.
Fill both blanks to replace the node's value with the inorder successor and delete it.
Node* temp = [1](root->right); root->val = temp->[2]; root->right = deleteNode(root->right, temp->val);
The inorder successor is the minimum value node in the right subtree, and we replace the current node's value with it.
Fill all three blanks to handle the case when the node has only one child.
if (root->left == nullptr) { Node* temp = root->[1]; delete root; return [2]; } else if (root->right == nullptr) { Node* temp = root->[3]; delete root; return temp; }
If the left child is null, we return the right child and vice versa. We store the child in temp, delete the current node, and return temp.