A Binary Search Tree is a binary tree where:
TreeNode* searchBST(TreeNode* root, int val) {
while (root != NULL && root->val != val ){
root = root->val < val ? root->right : root->left;
}
return root;
}
int floorInBST(TreeNode* root, int key){
int floor = -1;
while(root != nullptr){
if(root->val == key){
floor = root->val;
return floor;
}
if(key > root->val){
floor = root->val;
root = root->right;
} else {
root = root->left;
}
}
return floor;
}
int ceilInBST(TreeNode* root, int key){
int ceil = -1;
while(root != nullptr){
if(root->val == key){
ceil = root->val;
return ceil;
}
if(key > root->val){
root = root->right;
} else {
ceil = root->val;
root = root->left;
}
}
return ceil;
}