Binary tree : right view

when I am using int& maxlevel in the function it is working fine but when I use int maxlevel i.e. without reference it doesn’t work, could you tell me the use of reference variable in this scenerio.

void right(TreeNode* root,vector& v,int& maxlevel,int level){

    if(!root)return;
    
if(level>maxlevel){
    v.push_back(root->val);
maxlevel = level;
}
    right(root->right,v,maxlevel,level+1);
    right(root->left,v,maxlevel,level+1);
    
    
}

vector<int> rightSideView(TreeNode* root) {
    
    vector<int> v;
    int max = -1;
    right(root,v,max,0);
    return v;
}

It’s simple we are using pass by reference so that any computation we make in it should be stored in that variable only and also for all calls the same variable is judged. Such that when both the levels becomes equal we can increment it.
check this for better visualization:
pass-by-reference-vs-pass-by-value-animation .

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.