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

• Difficulty Level : Medium
• Last Updated : 22 Nov, 2022

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:

## C++

 `// C++ program to find the smallest value``// greater than or equal to N``#include ``using` `namespace` `std;` `struct` `Node {``    ``int` `data;``    ``Node *left, *right;``};` `// To create new BST Node``Node* createNode(``int` `item)``{``    ``Node* temp = ``new` `Node;``    ``temp->data = item;``    ``temp->left = temp->right = NULL;` `    ``return` `temp;``}` `// To add a new node in BST``Node* add(Node* node, ``int` `key)``{``    ``// if tree is empty return new node``    ``if` `(node == NULL)``        ``return` `createNode(key);` `    ``// if key is less than or greater than``    ``// node value then recur down the tree``    ``if` `(key < node->data)``        ``node->left = add(node->left, key);``    ``else` `if` `(key > node->data)``        ``node->right = add(node->right, key);` `    ``// return the (unchanged) node pointer``    ``return` `node;``}` `// function to find min value less than N``int` `findMinforN(Node* root, ``int` `N)``{``    ``// If leaf node reached and is smaller than N``    ``if` `(root->left == NULL && root->right == NULL``                                ``&& root->data < N)``        ``return` `-1;` `    ``// If node's value is greater than N and left value``    ``// is NULL or smaller then return the node value``    ``if` `((root->data >= N && root->left == NULL)``        ``|| (root->data >= N && root->left->data < N))``        ``return` `root->data;` `    ``// if node value is smaller than N search in the``    ``// right subtree``    ``if` `(root->right!=NULL and root->data <= N)``        ``return` `findMinforN(root->right, N);` `    ``// if node value is greater than N search in the``    ``// left subtree``    ``else` `if``(root->left!=NULL)``        ``return` `findMinforN(root->left, N);``}` `// Drivers code``int` `main()``{``    ``/*    19``        ``/    \``       ``7     21``     ``/   \``    ``3     11``         ``/   \``         ``9    14``          ``*/` `    ``Node* root = NULL;``    ``root = add(root, 19);``    ``root = add(root, 7);``    ``root = add(root, 3);``    ``root = add(root, 11);``    ``root = add(root, 9);``    ``root = add(root, 13);``    ``root = add(root, 21);` `    ``int` `N = 18;``    ``cout << findMinforN(root, N) << endl;` `    ``return` `0;``}`

## Java

 `// Java program to find the smallest value``// greater than or equal to N``import` `java.util.*;``class` `GFG``{``static` `class` `Node``{``    ``int` `data;``    ``Node left, right;``};` `// To create new BST Node``static` `Node createNode(``int` `item)``{``    ``Node temp = ``new` `Node();``    ``temp.data = item;``    ``temp.left = temp.right = ``null``;` `    ``return` `temp;``}` `// To add a new node in BST``static` `Node add(Node node, ``int` `key)``{``    ``// if tree is empty return new node``    ``if` `(node == ``null``)``        ``return` `createNode(key);` `    ``// if key is less than or greater than``    ``// node value then recur down the tree``    ``if` `(key < node.data)``        ``node.left = add(node.left, key);``    ``else` `if` `(key > node.data)``        ``node.right = add(node.right, key);` `    ``// return the (unchanged) node pointer``    ``return` `node;``}` `// function to find min value less than N``static` `int` `findMinforN(Node root, ``int` `N)``{``    ``// If leaf node reached and is smaller than N``    ``if` `(root.left == ``null` `&&``        ``root.right == ``null` `&& root.data < N)``        ``return` `-``1``;` `    ``// If node's value is greater than N and left value``    ``// is null or smaller then return the node value``    ``if` `((root.data >= N && root.left == ``null``) ||``        ``(root.data >= N && root.left.data < N))``        ``return` `root.data;` `    ``// if node value is smaller than N search in the``    ``// right subtree``    ``if` `(root.data <= N)``        ``return` `findMinforN(root.right, N);` `    ``// if node value is greater than N search in the``    ``// left subtree``    ``else``        ``return` `findMinforN(root.left, N);``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``/* 19``        ``/ \``    ``7     21``    ``/ \``    ``3     11``        ``/ \``        ``9 14``        ``*/` `    ``Node root = ``null``;``    ``root = add(root, ``19``);``    ``root = add(root, ``7``);``    ``root = add(root, ``3``);``    ``root = add(root, ``11``);``    ``root = add(root, ``9``);``    ``root = add(root, ``13``);``    ``root = add(root, ``21``);` `    ``int` `N = ``18``;``    ``System.out.println(findMinforN(root, N));``}``}` `// This code is contributed by Rajput-Ji`

## Python3

 `# Python program to find the smallest value``# greater than or equal to N` `class` `Node:``    ``def` `__init__(``self``):``        ``self``.data ``=` `0``        ``self``.left ``=` `None``        ``self``.right ``=` `None` `# To create new BST Node``def` `createNode(item: ``int``):``    ``temp ``=` `Node()``    ``temp.data ``=` `item``    ``temp.left ``=` `temp.right ``=` `None``    ``return` `temp` `# To add a new node in BST``def` `add(node: Node, key: ``int``):` `    ``# if tree is empty return new node``    ``if` `node ``is` `None``:``        ``return` `createNode(key)` `    ``# if key is less than or greater than``    ``# node value then recur down the tree``    ``if` `key < node.data:``        ``node.left ``=` `add(node.left, key)``    ``elif` `key > node.data:``        ``node.right ``=` `add(node.right, key)` `    ``# return the (unchanged) node pointer``    ``return` `node` `# function to find min value less than N``def` `findMinforN(root: Node, N: ``int``):` `    ``# If leaf node reached and is smaller than N``    ``if` `root.left ``is` `None` `and` `root.right ``is` `None` `and` `root.data < N:``        ``return` `-``1` `    ``# If node's value is greater than N and left value``    ``# is NULL or smaller then return the node value``    ``if` `root.data >``=` `N ``and` `root.left ``is` `None` `or` `root.data >``=` `N ``and` `root.left.data < N:``        ``return` `root.data` `    ``# if node value is smaller than N search in the``    ``# right subtree``    ``if` `root.data <``=` `N:``        ``return` `findMinforN(root.right, N)` `    ``# if node value is greater than N search in the``    ``# left subtree``    ``else``:``        ``return` `findMinforN(root.left, N)` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``'''``            ``19``        ``/ \``        ``7     21``        ``/ \``    ``3 11``        ``/ \``        ``9 14``    ``'''``    ``root ``=` `Node()``    ``root ``=` `add(root, ``19``)``    ``root ``=` `add(root, ``7``)``    ``root ``=` `add(root, ``3``)``    ``root ``=` `add(root, ``11``)``    ``root ``=` `add(root, ``9``)``    ``root ``=` `add(root, ``13``)``    ``root ``=` `add(root, ``21``)` `    ``N ``=` `18``    ``print``(findMinforN(root, N))` `# This code is contributed by``# sanjeev2552`

## C#

 `// C# program to find the smallest value``// greater than or equal to N``using` `System;` `class` `GFG``{``class` `Node``{``    ``public` `int` `data;``    ``public` `Node left, right;``};` `// To create new BST Node``static` `Node createNode(``int` `item)``{``    ``Node temp = ``new` `Node();``    ``temp.data = item;``    ``temp.left = temp.right = ``null``;` `    ``return` `temp;``}` `// To add a new node in BST``static` `Node add(Node node, ``int` `key)``{``    ``// if tree is empty return new node``    ``if` `(node == ``null``)``        ``return` `createNode(key);` `    ``// if key is less than or greater than``    ``// node value then recur down the tree``    ``if` `(key < node.data)``        ``node.left = add(node.left, key);``    ``else` `if` `(key > node.data)``        ``node.right = add(node.right, key);` `    ``// return the (unchanged) node pointer``    ``return` `node;``}` `// function to find min value less than N``static` `int` `findMinforN(Node root, ``int` `N)``{``    ``// If leaf node reached and is smaller than N``    ``if` `(root.left == ``null` `&&``        ``root.right == ``null` `&& root.data < N)``        ``return` `-1;` `    ``// If node's value is greater than N and left value``    ``// is null or smaller then return the node value``    ``if` `((root.data >= N && root.left == ``null``) ||``        ``(root.data >= N && root.left.data < N))``        ``return` `root.data;` `    ``// if node value is smaller than N search in the``    ``// right subtree``    ``if` `(root.data <= N)``        ``return` `findMinforN(root.right, N);` `    ``// if node value is greater than N search in the``    ``// left subtree``    ``else``        ``return` `findMinforN(root.left, N);``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``/* 19``        ``/ \``    ``7     21``    ``/ \``    ``3     11``        ``/ \``        ``9 14``        ``*/` `    ``Node root = ``null``;``    ``root = add(root, 19);``    ``root = add(root, 7);``    ``root = add(root, 3);``    ``root = add(root, 11);``    ``root = add(root, 9);``    ``root = add(root, 13);``    ``root = add(root, 21);` `    ``int` `N = 18;``    ``Console.WriteLine(findMinforN(root, N));``}``}` `// This code is contributed by Princi Singh`

## Javascript

 ``

Output

`19`

My Personal Notes arrow_drop_up