Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5761 |

@blackshadows | 5701 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4944 |

@saiujwal13083 | 4506 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3516 |

@sushant_a | 3459 |

@verma_ji | 3341 |

@KshamaGupta | 3318 |

Complete Leaderboard | |

Handle | Score |

@aroranayan999 | 938 |

@bt8816103042 | 739 |

@SHOAIBVIJAPURE | 428 |

@codeantik | 412 |

@SherlockHolmes3 | 407 |

@neverevergiveup | 348 |

@mahlawatep | 347 |

@shalinibhataniya1097 | 343 |

@murarry3625 | 333 |

@saiujwal13083 | 326 |

@rohitanand | 314 |

Complete Leaderboard |

Given a binary search tree and a number N, find the greatest number in the binary search tree that is less than or equal to N.

**Example 1 :**

**Input : **N = 24
**Output :** 21
**Explanation :** The greatest element in the tree which
is less than or equal to 24, is 21.
(Searching will be like 5->12->21)

**Example 2 :**

**Input :** N = 4
**Output :** 3
**Explanation :** The greatest element in the tree which
is less than or equal to 4, is 3.
(Searching will be like 5->2->3)

**Your task : **

You don't need to read input or print anything. Your task is to complete the function **findMaxForN()** which takes the root of the BST, and the integer N as input paramters and returns the greatest element less than or equal to N in the given BST.

**Expected Time Complexity: **O(Height of the BST)

**Expected Auxiliary Space: **O(Height of the BST).

**Constraints:**

1<=N<=10^{3}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Closest Neighbor in BST

...