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
- Nearest greater number by interchanging the digits
- Number of unique paths in tree such that every path has a value greater than K
- Find the kth smallest number with sum of digits as m
- Largest number in BST which is less than or equal to N
- Highest power of 2 less than or equal to given number
- Largest number less than or equal to N in BST (Iterative Approach)
- Root to leaf path sum equal to a given number
- Print all non-increasing sequences of sum equal to a given number x
- Root to leaf path sum equal to a given number in BST
- Shortest root to leaf path sum equal to a given number
- Root to leaf path product equal to a given number
- Find number of edges that can be broken in a tree such that Bitwise OR of resulting two trees are equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.