Biorąc pod uwagę binarne drzewo wyszukiwania i liczba całkowita K, chciałbym znaleźć największą elementu mniej niż K.
W poniższym drzewie,
for K = 13, result = 12
for K = 10, result = 8
for K = 1 (or) 2, result = -1
10
5 12
2 8 11 14
Próbowałem poniżej logikę. Ale czy jest jakiś lepszy sposób to zrobić?
int findNum(node* node, int K)
{
if(node == NULL)
{
return -1;
}
else if(K <= node->data)
{
return findNum(node->left,K);
}
else if(K > node->data)
{
int t = findNum(node->right,K);
return t > node->data ? t : node->data;
}
return -1;
}













