Kindly tell the problem in my code

kindly check the print right view fn, all the code except for it is working fine but this fn is giving weird output.

Hi Laksh. I have corrected ur code. It is working fine now.

I have also added another method for computing right view using map.
Hope your doubt is cleared now.

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.

bhaiya could you tell me what you have changed?
ig it’s pass by ref of the max_level;
if yes then why are we using it here?

yes. see here…every time we are updating it. but if its not pass by refence then max_level value always remains the same
image

there was one more mistake.
image
here u were making both the calls in the right subtree only

1 Like

bhaiya i am not getitng it , if the max_val has been updated from say -1 to 1 then for the next call the val passed will be 1 only na

i know you are right but i can’t get it why

that is why u have to keep it by reference so that its value is updated every time.
if u have further queries u should go through pass by refence and pass by value concepts once

1 Like

basically if its not pass by refence then if u are updating its value or not, in actual the value doesn’t get updated at the memory location.

bhaiya if we only consider say level 1 to level 2
function starts with level =1, maxlevel = -1;
it sees that the root is not null
then since level>maxlevel it prints 8 and assigns maxlevel value 1
then it makes a call recursively with parameters (root->right, 1+1, 1) isn’t it correct?

no it will happen only when value of max_level changes in memory. and for that u have to use pass by reference

so every time func is called it uses the data at the given memory location?

when variable are defined outside the function and there value is used in further recursive calls for computations

i this case the calls will use the memory at that particular location?

yes. max_level value is used in every call

okay i did not know this

kindly confirm my statement
" if the variable passed as parameter is used in the recursive calls in that case for each call the value of variable wii be the val stored at the memory location of that variable"

Laksh I strongly recommend u to go though pass by refence and pass by value concepts once. It will really help u a lot

yes check this for better visualization:
pass-by-reference-vs-pass-by-value-animation
in pass by value, another copy of variable is created but in pass by reference, same variable is sent in next calls. So updating that value will lead to update in all the future calling calls for that variable.