Returns largest value smaller than or equal to key . If key is smaller than the smallest , it returns - 1.
int findFloor ( Node * root , int key ) { Node * curr = root , * ans = NULL ; while ( curr ) { if ( curr -> key <= key ) { ans = curr ; curr = curr -> right ; } else curr = curr -> left ; } if ( ans ) return ans -> key ; return -1 ; }