Binary Search tree

https://hack.codingblocks.com/practice/p/355/143
For this question how I can manuplate this Sum replacement code.
to get the answer,I tried in many ways but I failed to do so

int replaceSum(node*root){
if(root==NULL){
return 0;
}
if(root->left==NULL && root->right==NULL){
return root->data;
}
//Recursive Part
int leftSum = replaceSum(root->left);
int rightSum = replaceSum(root->right);

int temp = root->data;
root->data = leftSum + rightSum;
return temp + root->data;

}

Hey Kamal, in this problem you are supposed to replace each node with the sum of all greater nodes in the given BST.

void replaceSum(node *root, int *sum) 
{ 
    // Base Case 
    if (root == NULL)  return; 
  
    // Recur for right subtree 
    replaceSum(root->right, sum); 
  
    // Now *sum has sum of nodes in right subtree, add 
    // root->data to sum and update root->data 
    *sum = *sum + root->data; 
    root->data = *sum; 
  
    // Recur for left subtree 
    replaceSum(root->left, sum); 
} 
  
// A wrapper over replaceSum() 
void replaceSumBST(node *root) 
{ 
    int sum = 0; 
    replaceSum(root, &sum); 
}

i tried upon this -but only one node is correct plz see this
int k=0;
void sumreplace(node* root)
{
if(root->left)
{
sumreplace(root->left);
k=k-root->data;
root->data=k;

}

k=k-root->data;
root->data=k;
if(root->right){
sumreplace(root->right);
k=k-root->data;
root->data=k;
}
if(root==NULL)
return;

}
int main() {

int n;
cin>>n;

node * root=NULL;
while(n!=-1){
    root=insert(root,n);
    k=k+n;
    cin>>n;
    
    
}
sumreplace(root);
print(root);
return 0;

}

Hey Kamal, can you please copy your whole code on ide save it, and share that link, as this code is incomplete which you have copied here.