Right View of Binary Tree

Not printing correctly can you tell me where it is getting wrong it is also printing the left tree.

Here is my code: https://ide.codingblocks.com/s/237403

The problem can also be solved using simple recursive traversal. We can keep track of level of a node by passing a parameter to all recursive calls. The idea is to keep track of maximum level also. And traverse the tree in a manner that right subtree is visited before left subtree. Whenever we see a node whose level is more than maximum level so far, we print the node because this is the last node in its level (Note that we traverse the right subtree before left subtree). Following is the implementation of this approach.

My approach is also same I didn’t asked your code I want you to tell where I am doing mistake in my code.
Because everything I did is as taught in the class

int max_level = -1;ko global banao and usko update kaaro 

print only if the currLevel > greater than max level

1 Like

Ok ,ha hogya max_level = -1 se Baar Baar initialize ho jarha tha isliye left view bhi print ho rha tha

ha exactly yeh glt ho raha tha!
are all test case passing 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.