Question: Write a function to find the minimum and maximum nodes within a BST and then replace the maximum node with the minimum node. Then, delete
Write a function to find the minimum and maximum nodes within a BST and then replace the maximum node with the minimum node. Then, delete the minimum node.
You should call the provided functions, findMinNode(node) and findMaxNode(node) to find the minimum node and maximum nodes in the tree; Call these functions within the function you write.
Header: void replaceMaxWithMin(TreeNode *root);
TreeNode struct:
struct TreeNode{
int key;
TreeNode *leftChild;
TreeNode *rightChild;
TreeNode *parent;
};
Functions to call to get the min and max:
TreeNode* findMinNode(TreeNode* min){
while (min->leftChild != NULL){ min = min->leftChild; }
return min;
}
TreeNode* findMaxNode(TreeNode* max){
while (max->rightChild != NULL){ max = max->rightChild; }
return max;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
