Sum Replacement

When we call bfs function by passing root as a parameter, a tree is printed. Then we call replaceSum function by passing root as a parameter. and then again we call bfs function which produces the result as expected.

My question is why are not passing by address of the root node in replaceSum function?? How the values are changing in the root node as they are not passed by address.

did you mean pass by reference??

yes by reference I mean :sweat_smile:

we are passing the address of root node
hence it is a kind of pass by reference
because all the changes will refelect back as we are doing it by address
NOT making any copy

But if a function parameter is as follows
replaceSum(node* root);
and other format as follows
replaceSum(node* &root);
Does both of these formats means same??

yes both should work similarly

are you getting some problem??

Yes,
I am getting confused with the following concept which I studied in Linked list:
As we insert at head in a linked list, we pass as follows:
void insertAtHead(node* &head, int data);
while we print a linked list, we pass as follows:
void print(node* head);
Now, my question is both the above formats are same why are adding ampersand symbol (&)?

okay i got your question now

void insertAtHead(node* &head, int data);
here we also want to update the value of head variable so we pass head by reference

void print(node* head);
here we didn’t update anything so head should not change
hence we don’t use &

in the print function as well if we do head->data =newdata ;
then head’s data will change
but if we do head=newhead;
then it will not update head

in insertAtHead both things update

1 Like

ok now I got your point and understood. Thanks…

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.

now plz give your valuable feedback