Binary tree right view

jaab humare leaf node ke right aur left mai NULL hoga toh
printRightView(root->right, level+1, maxlevel);
printRightView(root->left, level+1, maxlevel);
iske according us case mai level mai +1 hoga yaa ni?
kyuki dry run karne im getting confused

Yes level will be increased by 1. But it does not matter because once the root node is NULL it will enter the base and return from there to the previous state

  1. starting mai max level -1 hai or level 0 hai fir humne jab main mai function call kia toh maxlevel<level hua aur 1 print hua fir maxlevel = level hua toh maxlevel becomes 0.

2.1 ke right mai we called function again and level mai 1 add hua toh we have currently ,maxlevel=0 and level = 1 and maxlevel <level satisfy hua and 3 print hua then maxlevel=level hua toh maxlevel becomes 1

3.now maxlevel aur level dono 1 hai fir 3 ke right me we recursion hua and level 2 hua kyuki vaha null hai toh return hua and maxlevel same raha i.e maxlevel=1 and level=2 hai

4.similarly return hone ke baad 3 ke left mai bhi null hai toh level+1 hua and level becomes 3
now we have max level=1 and we return back to 1.

  1. fir 1 ke left mai function dobara call hua aab vaha 2 present hai and our max level =1 and level mai dobara+1 hua and level becomes=4 or hara maxlevel<level hai toh 2 print hona chaiye thaa naa vo nahi hua?

toh dry run karne par kuch aur horaha hai

Some Corrections:

At node 3, your level was 1(So for node 3 level is 1). Now you recursively for right subtree but the right node of 3 is NULL so hits the base case and return from there. Now you have returned to node 3, so level =1 and it is not equal to 2(since level was passed by value so level will return to its original state that was at node 3).

Keep this in mind, dry run it again and check.

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.