Doubt in right view

void rightViewUtil(node*root,int level,int *max_level)
{
if(root==NULL)
{
return;
}
if(*max_level<level)
{
cout<data<<" ";
*max_level=level;
}
rightViewUtil(root->right,level+1,max_level);
rightViewUtil(root->left,level+1,max_level);
}
In this max_level id passed by reference.But when passed by parameter output comes wrong.Can you please tell why it is happening.

Hello @Hk199977,

When you pass a variable by reference, the changes made in it’s value stays.
And when you pass it by value then a copy of that variable is made.
So, the changes made in the copy would not reflect back in the original variable.

Here, max_level is used keep track of the maximum level up to which we have traversed in the code.
So, if you will pass it by value, then updates made in it’s value will not visible for other function calls in the call stack.
Hence, leading to wrong error.

Hope, this would help.
Give a like if you are satisfied.

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.