Replace with sum of greater nodes bst

i made the bst
changed the nodes data according to qn
printed preorder and still my output doesnt match the output of given test case

https://ide.codingblocks.com/s/45142

https://hack.codingblocks.com/contests/c/511/1606

Hey Jai, don’t use array to get the sum for the current node do this by recursion only. This is the only reason because of which your output is not correct.

my output format isnt correct
and
if i go with rec then i will alter the nodes data and this will result in unwanted alteration in data of other nodes
bcoz now there will be different data at each node
basically what i mean is that i will lose the original data of nodes if i replace them and hence will calculate wrong new data for each node
i had tried but the above thing happened

Hey Jai, to resolve this you can store the data of that node in some reference variable of an array of size 1 (as shown in eg. below), by doing this you will not lose the actual data of the node. You can refer the code snippet given below.

void modifyBSTwithgreaterelementssum(Node node, int[] sum) {
		if(node == null)
			return;
		modifyBSTwithgreaterelementssum(node.right, sum);
		sum[0]+=node.data;
		node.data = sum[0];
		modifyBSTwithgreaterelementssum(node.left, sum);
	}