Doubt in code...../////

Please check my code of building a tree. How can i correct it.

Hi @Aparna
you have take input only once in line no 46
you have to take complete input

and why you make queue
there is no need of queue

please try again
if you stuck in any part you can ask

because one of The TA send me the link of gfg code and asked me to refer that, There they are using queue. Please tell me the code for this. i am stuck

your code is not building tree correctly
first build tree

Node* BuildTree(){
	int d;cin>>d; /// take input 
	if(d==-1)return NULL;      // if it is -1 means no further nodes so return NULL
	Node*nn=new Node(d);      // else create node with data d
	nn->left=BuildTree();      // now construct left part of node using recursion
	nn->right=BuildTree();    // now construct left part of node using recursion
	return nn;                // after constructin root ,left ,right return node address which will store in root of tree inside main()
}

int main(){
node*root=buildTree();
}
this is simple code to build bst

now for solving this question

we do reverse Inorder traversal of BST, we get all nodes in decreasing order. We do reverse Inorder traversal and keep track of the sum of all nodes visited so far, we add this sum to every node.

let us see
/ Recursive function to add all greater values in every node

void modifyBST(Node *root, int &sum)

{

// Base Case
if (root == NULL) return ;
// now Recur for right subtree

modifyBST(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
modifyBST(root->left, sum);
}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.