Replace with sum of greater node

private 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);
}

how this function replace node with greater element??
in this function we pass sum and node as recursive call and after calling it changes the sum[0] value but function does not return any thing when recursive call complete then previous function sum value is not change, why it is change?

hi @abhishekg
sum[0] is acting as a global variable its not necessary to use a an array.
sum[0] is storing the sum of every node as it travels and updating the value the at node.

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.

when function call back then then it destroy all variable and array then how it work as global variable please explain ???

@abhishekg,
the scope of the global variable is through out the code. If you call a function, the values in the global variable remain intact and are updated with every execution of the code instead of being destroyed when the function ends.

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.