How to take the inputs?

how i can build the tree using bfs level by level

node *buildtree()
{
    int a;
    cin>>a;
    node *root = new node(a);
    queue<node*> q;
    q.push(root);

    while(!q.empty())
    {
        cin>>a;
        if(a!=-1)
        {
            node *temp = new node(a);
            if(q.front()->left==NULL)
            q.front()->left = temp;
            else
            {
                q.front()->right = temp;
                q.pop();
            }
            q.push(temp);
        }
        else
        {
            if(q.front()->left==NULL)
            {
                cin>>a;
                if(a==-1)
                    q.pop();
                else
                {
                    node *temp1=new node(a);
                    q.front()->right = temp1;
                    q.pop();
                    q.push(temp1);
                }
            }
            else
                q.pop();
        }
    }
    return root;
}

int main()
{
    int h;
    cin>>h;
    node *root = buildtree();
}

This piece of code should help.

node * levelOrderInput() {

// Create Queue to store the left and the right childrens 
int data; cin >> data; 

queue<node *> q; 
node *root = new node(data); 
q.push(root); 

while(!q.empty()) {

    node *f = q.front(); 
    q.pop(); 

    int c1, c2; 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; 

}

this one is correct??

And one for question :
for(auto p : m) {
for (int x : p.second) {
cout << x << " ";
}
cout << endl;
}

if i used this notations p->second instead of p.second it showing me error

You can try and check for different inputs. You have both the codes now so you can do it by yourself.

p.second is used for pairs and p->second is used for map iterators.

@S19APP-PP0108 okay got it …

helperVerticalOrder(root, 0, m);
vector<vector> v;
// Print the value of the hashmaps
for(auto p : m) {
vector temp;
for(int x : p.second) {
temp.push_back(x);
}
v.push_back(temp);
}

for(auto it = v.begin(); it != v.end(); it++) {
    // 
}

how i can insert the value in the 2 d vectors i am little confused how to iterate in vectors of vectors

Declare a 2D vector with size only like - vector<vector > v(n,vector(m));
Then you can use it like a 2D array only.

for(int i=0; i<n; i++)
     for(int j=0; j<m; j++)
        cin>>v[i][j]

okay got it @S19APP-PP0108

Cool. Please mark your doubt as resolved 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.