We have a binary search tree and a number N. Our task is to find the greatest number in the binary search tree that is less than or equal to N. Print the value of the element if it exists otherwise print -1.

Examples:For the above given binary search tree-
Input : N = 24 Output :result = 21 (searching for 24 will be like-5->12->21) Input : N = 4 Output : result = 3 (searching for 4 will be like-5->2->3)
We have discussed recursive approach in below post.
Largest number in BST which is less than or equal to N
Here an iterative approach is discussed. We try to find the predecessor of the target. Keep two pointers, one pointing to the current node and one for storing the answer. If the current node’s data > N, we move towards left. In other case, when current node’s data is less than N, the current node can be our answer (so far), and we move towards right.
C++
// C++ code to find the largest value smaller // than or equal to N #include <bits/stdc++.h> using namespace std; struct Node { int key; Node *left, *right; }; // To create new BST Node Node* newNode(int item) { Node* temp = new Node; temp->key = item; temp->left = temp->right = NULL; return temp; } // To insert a new node in BST Node* insert(Node* node, int key) { // if tree is empty return new node if (node == NULL) return newNode(key); // if key is less then or grater then // node value then recur down the tree if (key < node->key) node->left = insert(node->left, key); else if (key > node->key) node->right = insert(node->right, key); // return the (unchanged) node pointer return node; } // Returns largest value smaller than or equal to // key. If key is smaller than the smallest, it // returns -1. int findFloor(Node* root, int key) { Node *curr = root, *ans = NULL; while (curr) { if (curr->key <= key) { ans = curr; curr = curr->right; } else curr = curr->left; } if (ans) return ans->key; return -1; } // Driver code int main() { int N = 25; Node* root = insert(root, 19); insert(root, 2); insert(root, 1); insert(root, 3); insert(root, 12); insert(root, 9); insert(root, 21); insert(root, 19); insert(root, 25); printf("%d", findFloor(root, N)); return 0; } |
Java
// Java code to find the largest value smaller // than or equal to N class GFG { static class Node { int key; Node left, right; }; // To create new BST Node static Node newNode(int item) { Node temp = new Node(); temp.key = item; temp.left = temp.right = null; return temp; } // To insert a new node in BST static Node insert(Node node, int key) { // if tree is empty return new node if (node == null) return newNode(key); // if key is less then or grater then // node value then recur down the tree if (key < node.key) node.left = insert(node.left, key); else if (key > node.key) node.right = insert(node.right, key); // return the (unchanged) node pointer return node; } // Returns largest value smaller than or equal to // key. If key is smaller than the smallest, it // returns -1. static int findFloor(Node root, int key) { Node curr = root, ans = null; while (curr != null) { if (curr.key <= key) { ans = curr; curr = curr.right; } else curr = curr.left; } if (ans != null) return ans.key; return -1; } // Driver code public static void main(String[] args) { int N = 25; Node root = new Node(); insert(root, 19); insert(root, 2); insert(root, 1); insert(root, 3); insert(root, 12); insert(root, 9); insert(root, 21); insert(root, 19); insert(root, 25); System.out.printf("%d", findFloor(root, N)); } } /* This code is contributed by PrinciRaj1992 */ |
C#
// C# code to find the largest value smaller // than or equal to N using System; using System.Collections.Generic; class GFG { public class Node { public int key; public Node left, right; }; // To create new BST Node static Node newNode(int item) { Node temp = new Node(); temp.key = item; temp.left = temp.right = null; return temp; } // To insert a new node in BST static Node insert(Node node, int key) { // if tree is empty return new node if (node == null) return newNode(key); // if key is less then or grater then // node value then recur down the tree if (key < node.key) node.left = insert(node.left, key); else if (key > node.key) node.right = insert(node.right, key); // return the (unchanged) node pointer return node; } // Returns largest value smaller than or equal to // key. If key is smaller than the smallest, it // returns -1. static int findFloor(Node root, int key) { Node curr = root, ans = null; while (curr != null) { if (curr.key <= key) { ans = curr; curr = curr.right; } else curr = curr.left; } if (ans != null) return ans.key; return -1; } // Driver code public static void Main(String[] args) { int N = 25; Node root = new Node(); insert(root, 19); insert(root, 2); insert(root, 1); insert(root, 3); insert(root, 12); insert(root, 9); insert(root, 21); insert(root, 19); insert(root, 25); Console.Write("{0}", findFloor(root, N)); } } // This code is contributed by Rajput-Ji |
Output:
25
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Largest number in BST which is less than or equal to N
- Inorder predecessor and successor for a given key in BST | Iterative Approach
- K'th Largest Element in BST when modification to BST is not allowed
- Largest subset having with sum less than equal to sum of respective indices
- Smallest number in BST which is greater than or equal to N
- Count the number of unordered triplets with elements in increasing order and product less than or equal to integer X
- Pair with largest sum which is less than K in the array
- Find largest factor of N such that N/F is less than K
- Queries for greater than and not less than
- Sum of all array elements less than X and greater than Y for Q queries
- Check if a Binary Tree is BST : Simple and Efficient Approach
- Find k-th smallest element in BST (Order Statistics in BST)
- Two nodes of a BST are swapped, correct the BST
- Convert a normal BST to Balanced BST
- Two nodes of a BST are swapped, correct the BST | Set-2
- For each element in 1st array count elements less than or equal to it in 2nd array
- Maximum sum subarray having sum less than or equal to given sum using Set
- Find all possible subarrays having product less than or equal to K
- Number of elements smaller than root using preorder traversal of a BST
- Length of longest subarray in which elements greater than K are more than elements not greater than K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

