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