Right view of binary tree

i just want to know difference between these two function

code 1

void rview(treenode *root,int level,int *maxlevel)
{

if(root==NULL)
return;

if(*maxlevel<level)
{
    *maxlevel=level;
    cout<<root->val<<" ";
}

rview(root->right,level+1,maxlevel);
rview(root->left,level+1,maxlevel);

}

void right_view(treenode *root)
{

int maxlevel=-1;

rview(root,1,maxlevel);

}

code 2

void rview(treenode *root,int level,int maxlevel)
{

if(root==NULL)
return;

if(maxlevel<level)
{
    maxlevel=level;
    cout<<root->val<<" ";
}

rview(root->right,level+1,maxlevel);
rview(root->left,level+1,maxlevel);

}

void right_view(treenode *root)
{

int maxlevel=-1;

rview(root,1,maxlevel);

}

both this function give different output in below code

i know the concept of pass by value and pass by reference

hello @aattuull
In code 1-> max_level is passed as value i.e copy of max_level is passed due to which whatever changes we make with max_level will not be reflected in actual max_level due to which u will not get actual output.

In code 2->
max_level is passed as pointer ie address of max_level is pased so whatever changes function does on max_level will happen on same max_level and thus it will produces the required output.

sorry my doubt is not clear
i already know the concept of pass by value and pass by reference and also in both code 1 & 2 max_level show same values
i just want to know when i use code 1 ,it will give correct output i.e show right view of tree but for code 2 it will print all tree node

there is difference in output only because of pass by value and pass by pointer.

no u will not get same values
run both code and print max_value in each call u will get the difference

i already try to print max_value with both above codes, in both the case i get same values

print after line 43. (ie after calling function to its right)

1 Like

thanks my doubt is clear now