We are given input as level-order traversal, so i’m first placing it in a vector , where no. of -1 nodes is n+1 the number of non -1 nodes, then i’m accessing the level-order using queue.
i’m only passing a few of the test cases. Please help!
Bottom view of the tree
You need to use level order for building your tree instead of simply pushing it into the vector…
Try using this approach for building your tree correctly as
queue<node *>q;
int d;
cin>>d;
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);
}
}
return root;