************************how to take level order input
Right view of binary tree
Level order input is taken and tree is build by using the queue, in which firstly u will take a queue of nodes, and then it will have a data which will represent a root node, and then u will accept the left and right child of root node, -1 value means that the node does not have any child.
U can refer to this code for your help
@ayu2321 hope its cleared if yes dont forget to mark the doubt as resolved
what is wrong in this code
my cb ide donot work pls check this
#include
#include
using namespace std;
class node{
public:
int data;
node *left;
node *right;
node(int d){
data = d;
left = NULL;
right = NULL;
}
};
node *buildtreeLevel()
{
int d;
cin>>d;
queue<node *>q;
node *root=new node(d);
q.push(root);
int c1,c2;
while(!q.empty())
{
node *f=q.front();
q.pop();
cin>>c1>>c2;
if(c1!=-1)
{
f->left=new node(c1);
q.push(f->left);
}
if(c2!=-1)
{
f->right=new node(c2);
q.push(f->right);
}
}
}
void rightView(node *root, int level, int &maxlevel){
if(root==NULL){
return;
}
if(maxlevel<level){
cout<data<<" ";
maxlevel = level;
}
rightView(root->right,level+1,maxlevel);
rightView(root->left,level+1,maxlevel);
}
int main(){
node *root = buildtreeLevel();
int maxlevel = -1;
rightView(root,0,maxlevel);
return 0;}