VOOZH about

URL: https://www.geeksforgeeks.org/dsa/smallest-number-in-bst-which-is-greater-than-or-equal-to-n/

⇱ Smallest number in BST which is greater than or equal to N - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Smallest number in BST which is greater than or equal to N

Last Updated : 11 Jul, 2025

Given a Binary Search Tree and a number N, the task is to find the smallest number in the binary search tree that is greater than or equal to N. Print the value of the element if it exists otherwise print -1.

Examples: 

Input: N = 20 
Output: 21 
Explanation: 21 is the smallest element greater than 20.

Input: N = 18 
Output: 19 
Explanation: 19 is the smallest element greater than 18. 

Approach: 
The idea is to follow the recursive approach for solving the problem i.e. start searching for the element from the root. 

  • If there is a leaf node having a value less than N, then element doesn't exist and return -1.
  • Otherwise, if node's value is greater than or equal to N and left child is NULL or less than N then return the node value.
  • Else if node's value is less than N, then search for the element in the right subtree.
  • Else search for the element in the left subtree by calling the function recursively according to the left or right value.

Implementation:


Output
19
Comment