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.
Input: N = 20
Explanation: 21 is the smallest element greater than 20.
Input: N = 18
Explanation: 19 is the smallest element greater than 18.
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.
- Smallest number in BST which is greater than or equal to N ( Iterative Approach)
- Smallest Greater Element on Right Side
- Number of nodes greater than a given value in n-ary tree
- Largest number in BST which is less than or equal to N
- Highest power of 2 less than or equal to given number
- Root to leaf path sum equal to a given number
- Root to leaf path sum equal to a given number in BST
- Largest number less than or equal to N in BST (Iterative Approach)
- Print all non-increasing sequences of sum equal to a given number x
- Root to leaf path product equal to a given number
- Add all greater values to every node in a given BST
- Transform a BST to greater sum tree
- Count greater nodes in AVL tree
- Find closest greater value for every element in array
- Closest greater or same value on left side for every element in array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.