How to take input tree

i know how to solve… facing some difficulty in taking input.
right view

1 2 3 4 5 -1 6 -1 -1 -1 -1 -1 -1

1 is root node
2 is left chlid
    3 is left of 2
         4 is left of 3
              5 is left of 4
                -1 represent no node in left of 5
                 6 is right of 5
                -1  -1 represent no node in Left and Right of 6
             -1  nothing is right of 4
         -1  nothing is right of 3
     -1  nothing is right of 2
-1  nothing is right of 1

use recursion to take input

i dont think this we have to do this way…
in ques they are asking to take input in level order

okay okay
if it is mention to take input in level order
then do in this manner

int i=0
    queue<node*>q;
    q.push(arr[i++]);
    while(!q.empty()){
        node*temp=q.front();
        q.pop();
        else{
            int leftchild=arr[i++],rightchild=arr[i++];
            if(leftchild!=-1){
                temp->left=new node(leftchild);
                q.push(temp->left);
            }
            else temp->left=NULL;
            if(rightchild!=-1){
                temp->right=new node(rightchild);
                q.push(temp->right);
            }
            else temp->right=NULL;
        }
    }

thanks a lot it helped

1 Like

if your doubt is resolved
please mark it as resolved