[Approach 1] Using Recursion - O(h) Time and O(h) Space
The idea is to find the ceil value recursively: if the current node’s value is greater than or equal to x, consider it as a answer and move left for a closer value; otherwise, move right to search for a larger value.
Steps to solve the problem:
Set ans = -1
While node exists:
If node->data == key, return key.
If key < node->data, set ans = node->data and move left (maybe there’s a smaller valid value).
Else (key > node->data), move right (need a bigger value).
Return ans.
C++
#include<iostream>usingnamespacestd;structNode{intkey;Node*left;Node*right;Node(intvalue){key=value;left=right=nullptr;}};// Function to find the ceiling of a given // input in BST. If the input is more than// the max key in BST, return -1.intfindCeil(Node*root,intx){// Base caseif(root==nullptr)return-1;// We found equal keyif(root->key==x)returnroot->key;// If root's key is smaller, // ceil must be in the right subtreeif(root->key<x)returnfindCeil(root->right,x);// Else, either left subtree or// root has the ceil valueintceil=findCeil(root->left,x);return(ceil>=x)?ceil:root->key;}intmain(){Node*root=newNode(8);root->left=newNode(4);root->right=newNode(12);root->left->left=newNode(2);root->left->right=newNode(6);root->right->left=newNode(10);root->right->right=newNode(14);intx=11;cout<<findCeil(root,x)<<endl;return0;}
C
#include<stdio.h>#include<stdlib.h>// Structure of each Node in the treestructNode{intkey;structNode*left;structNode*right;};// Function to find the ceiling of a given // input in BST. If the input is more than// the max key in BST, return -1.intfindCeil(structNode*root,intx){// Base caseif(root==NULL)return-1;// We found equal keyif(root->key==x)returnroot->key;// If root's key is smaller, // ceil must be in the right subtreeif(root->key<x)returnfindCeil(root->right,x);// Else, either left subtree or// root has the ceil valueintceil=findCeil(root->left,x);return(ceil>=x)?ceil:root->key;}// Function to create a new nodestructNode*newNode(intkey){structNode*node=(structNode*)malloc(sizeof(structNode));node->key=key;node->left=node->right=NULL;returnnode;}intmain(){structNode*root=newNode(8);root->left=newNode(4);root->right=newNode(12);root->left->left=newNode(2);root->left->right=newNode(6);root->right->left=newNode(10);root->right->right=newNode(14);intx=11;printf("%d",findCeil(root,x));return0;}
Java
classNode{intkey;Nodeleft,right;Node(intvalue){key=value;left=right=null;}}// Function to find the ceiling of a given input in BST.// If the input is more than the max key in BST, return -1.publicclassGfG{staticintfindCeil(Noderoot,intx){// Base caseif(root==null){return-1;}// We found equal keyif(root.key==x){returnroot.key;}// If root's key is smaller, // ceil must be in the right subtreeif(root.key<x){returnfindCeil(root.right,x);}// Else, either left subtree or root// has the ceil valueintceil=findCeil(root.left,x);return(ceil>=x)?ceil:root.key;}publicstaticvoidmain(String[]args){Noderoot=newNode(8);root.left=newNode(4);root.right=newNode(12);root.left.left=newNode(2);root.left.right=newNode(6);root.right.left=newNode(10);root.right.right=newNode(14);intx=11;System.out.println(findCeil(root,x));}}
Python
classNode:def__init__(self,value):self.key=valueself.left=Noneself.right=None# Function to find the ceiling of a given input in BST.# If the input is more than the max key in BST, return -1.deffindCeil(root,x):# Base caseifrootisNone:return-1# We found equal keyifroot.key==x:returnroot.key# If root's key is smaller, # ceil must be in the right subtreeifroot.key<x:returnfindCeil(root.right,x)# Else, either left subtree or root has the ceil valueceil=findCeil(root.left,x)returnceilifceil>=xelseroot.keyif__name__=="__main__":root=Node(8)root.left=Node(4)root.right=Node(12)root.left.left=Node(2)root.left.right=Node(6)root.right.left=Node(10)root.right.right=Node(14)x=11;print(findCeil(root,x));
C#
usingSystem;classNode{publicintKey;publicNodeLeft,Right;publicNode(intvalue){Key=value;Left=Right=null;}}classGfG{// Function to find the ceiling of a given input in BST.// If the input is more than the max key in BST, return -1.staticintfindCeil(Noderoot,intx){// Base caseif(root==null){return-1;}// We found equal keyif(root.Key==x){returnroot.Key;}// If root's key is smaller, // ceil must be in the right subtreeif(root.Key<x){returnfindCeil(root.Right,x);}// Else, either left subtree or root // has the ceil valueintceil=findCeil(root.Left,x);return(ceil>=x)?ceil:root.Key;}staticvoidMain(){Noderoot=newNode(8);root.Left=newNode(4);root.Right=newNode(12);root.Left.Left=newNode(2);root.Left.Right=newNode(6);root.Right.Left=newNode(10);root.Right.Right=newNode(14);intx=11;Console.WriteLine(findCeil(root,x));}}
JavaScript
classNode{constructor(key){this.key=key;this.left=null;this.right=null;}}// Function to find the ceiling of a given input in BST.// If the input is more than the max key in BST, return -1.functionfindCeil(root,x){// Base caseif(root===null){return-1;}// We found equal keyif(root.key===x){returnroot.key;}// If root's key is smaller, // ceil must be in the right subtreeif(root.key<x){returnfindCeil(root.right,x);}// Else, either left subtree or root has the ceil valueconstceil=findCeil(root.left,x);return(ceil>=x)?ceil:root.key;}// Driver codeconstroot=newNode(8);root.left=newNode(4);root.right=newNode(12);root.left.left=newNode(2);root.left.right=newNode(6);root.right.left=newNode(10);root.right.right=newNode(14);letx=11;console.log(findCeil(root,x));
Output
12
Time complexity: O(h) where h is height of the given BST Auxiliary Space: O(h)
[Approach 2] Using Iterative Way - O(h) Time and O(1) Space
The idea is to traverse the BST: if the current node’s value is ≥ x, move left to try for a smaller valid value; otherwise, move right to look for a greater value.
Below is the implementation of the above approach:
C++
#include<iostream>usingnamespacestd;structNode{intdata;Node*left,*right;Node(intvalue){data=value;left=right=nullptr;}};// Helper function to find ceil of a given key in BSTintfindCeil(Node*root,intx){intceil=-1;while(root){// If root itself is ceilif(root->data==x){returnroot->data;}// If root is smaller, the ceil// must be in the right subtreeif(x>root->data){root=root->right;}// Else either root can be ceil// or a node in the left childelse{ceil=root->data;root=root->left;}}returnceil;}// Driver codeintmain(){Node*root=newNode(8);root->left=newNode(4);root->right=newNode(12);root->left->left=newNode(2);root->left->right=newNode(6);root->right->left=newNode(10);root->right->right=newNode(14);intx=11;cout<<findCeil(root,x);return0;}
C
#include<stdio.h>#include<stdlib.h>// Structure for a tree nodestructNode{intdata;structNode*left;structNode*right;};// Helper function to find ceil of a given// key in BSTintfindCeil(structNode*root,intx){intceil=-1;// -1 indicates no ceiling found yetwhile(root){// If root itself is ceilif(root->data==x){returnroot->data;}// If root is smaller, the ceil // must be in the right subtreeif(x>root->data){root=root->right;}// Else either root can be ceil // or a node in the left childelse{ceil=root->data;root=root->left;}}returnceil;}// Function to create a new nodestructNode*newNode(intx){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=x;node->left=node->right=NULL;returnnode;}// Driver codeintmain(){structNode*root=newNode(8);root->left=newNode(4);root->right=newNode(12);root->left->left=newNode(2);root->left->right=newNode(6);root->right->left=newNode(10);root->right->right=newNode(14);intx=11;printf("%d",findCeil(root,x));return0;}
Java
classNode{intdata;Nodeleft,right;Node(intvalue){data=value;left=right=null;}}// Function to find the ceiling of a given// input in BST. If the input is more than// the max key in BST, return -1.classGfG{staticintfindCeil(Noderoot,intx){intceil=-1;// -1 indicates no ceiling found yetwhile(root!=null){// If root itself is ceilif(root.data==x){returnroot.data;}// If root's data is smaller, // ceil must be in the right subtreeif(x>root.data){root=root.right;}// Else either root can be ceil // or a node in the left childelse{ceil=root.data;root=root.left;}}returnceil;}publicstaticvoidmain(String[]args){Noderoot=newNode(8);root.left=newNode(4);root.right=newNode(12);root.left.left=newNode(2);root.left.right=newNode(6);root.right.left=newNode(10);root.right.right=newNode(14);intx=11;System.out.println(findCeil(root,x));}}
Python
classNode:def__init__(self,value):self.data=valueself.left=Noneself.right=None# Helper function to find the ceiling# of a given x in BSTdeffindCeil(root,x):ceil=-1# -1 indicates no ceiling found yetwhileroot:# If root itself is ceilifroot.data==x:returnroot.data# If root's data is smaller, # ceil must be in the right subtreeifx>root.data:root=root.rightelse:# Else either root can be ceil # or a node in the left childceil=root.dataroot=root.leftreturnceil# Driver coderoot=Node(8)root.left=Node(4)root.right=Node(12)root.left.left=Node(2)root.left.right=Node(6)root.right.left=Node(10)root.right.right=Node(14)x=11;print(findCeil(root,x));
C#
usingSystem;classNode{publicintData;publicNodeLeft,Right;publicNode(intvalue){Data=value;Left=Right=null;}}classGfG{// Function to find the ceiling of a given input in BST.// If the input is more than the max key in BST, return -1.staticintfindCeil(Noderoot,intx){intceil=-1;// -1 indicates no ceiling found yetwhile(root!=null){// If root itself is ceilif(root.Data==x){returnroot.Data;}// If root's data is smaller, // ceil must be in the right subtreeif(x>root.Data){root=root.Right;}// Else either root can be ceil // or a node in the left childelse{ceil=root.Data;root=root.Left;}}returnceil;}staticvoidMain(){Noderoot=newNode(8);root.Left=newNode(4);root.Right=newNode(12);root.Left.Left=newNode(2);root.Left.Right=newNode(6);root.Right.Left=newNode(10);root.Right.Right=newNode(14);intx=11;Console.WriteLine(findCeil(root,x));}}
JavaScript
classNode{constructor(value){this.data=value;this.left=null;this.right=null;}}// Helper function to find the ceil of a given // x in BSTfunctionfindCeil(root,x){letceil=-1;// -1 indicates no ceiling found yetwhile(root){// If root itself is ceilif(root.data===x){returnroot.data;}// If root is smaller, the ceil// must be in the right subtreeif(x>root.data){root=root.right;}// Else either root can be ceil // or a node in the left childelse{ceil=root.data;root=root.left;}}returnceil;}// Driver codeconstroot=newNode(8);root.left=newNode(4);root.right=newNode(12);root.left.left=newNode(2);root.left.right=newNode(6);root.right.left=newNode(10);root.right.right=newNode(14);letx=11;console.log(findCeil(root,x));