Minimum and Maximum element in BST
//If the given root node is empty, the return -1, else move to the left and left and left-est child of the root, which will be the minimum value
//Similarly, for maximum value, move to the rightest child for every node, until last right node is achieved,
which will be the value in the BST.
which will be the value in the BST.
int minValue(Node* root)
{
if(root==0)
return -1;
else if (root->left==0)
return root->data;
else
return minValue(root->left);
}
if(root==0)
return -1;
else if (root->left==0)
return root->data;
else
return minValue(root->left);
}
int maxValue(Node* root)
{
if(root==0)
return -1;
else if (root->right==0)
return root->data;
else
return maxValue(root->right);
}
Comments
Post a Comment