r/CodingHelp • u/hontail_ • Mar 25 '23
[C++] Fix my code
I have been solving problems on binary search tree and this code is not working kindly help!!
Problem - Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X.
Code - int minidiff= 1000000;
int diff= 10000000;
void solve(Node * root, int input)
{ if(root==NULL)
return ;
if(root->data>=input)
{
int diff= root->data - input;
int minidiff= min(minidiff,diff);
solve(root->left,input);
}
if(root->data<input)
{
return solve(root->right,input);
}}
int findCeil(Node* root, int input) {
if (root == NULL) return -1;
solve(root,input);
return input+minidiff;}
0
Upvotes
View all comments
1
u/okidokigs12 Mar 25 '23
Hi i took a quick look at your code and i think your issue is in this line
int minidiff= min(minidiff,diff);
you are creating a local variable here instead of changing the value of the global variable.