I tried to print right side of the tree but it is printing all nodes what i did wrong

in your code you go to right as well left subtree
ans also increase level so every time maxlevel is less than level hence print all nodes
your approach is not correct
use this:
The Right view contains all nodes that are last nodes in their levels. A simple solution is to do level order traversal and print the last node in every level.