Right view of tree

please remove the problem from my code why im not getting ans

#include
#include
using namespace std;
int k=-1;
class node{
public:
int data;
node* left;
node* right;
node(int d){
data=d;
left=NULL;
right=NULL;
}

};

void rightview(node *root,int n){
if(root==NULL){
return ;
}
if(n>k){
cout<data<<" ";
k++;
}
rightview(root->right,n+1);

rightview(root->left,n+1);

}

node* insertvalue(node root,int value,queue<node> &q,int &count){
node*a;
if(value!=-1){
a=new node(value);
}
else{
a=NULL;

if(q.front()->left==NULL){

q.front()->left=a;
count++;

}

else if(q.front()->right==NULL){

q.front()->right=a;
q.pop();
}

if(value!=-1){
q.push(a);
}
return root;

}

int main() {

queue<node*> q;
int x;
cin>>x;

node* root=new node(x);
q.push(root);
int count =0;
while(!q.empty()){
int y;
cin>>y;
root=insertvalue(root,y,q,count);
}
rightview(root,0);

return 0;

}

@deepakjumani09 hey bro inplace of k++ do k=n; you will get it correct.

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.