How can I input an array of unknown size?
Unknown array size
you need not to store this in an array
take input make a node and insert in tree
you can see this
node* BuildTree(){
int d;cin>>d;
if(d==-1)return NULL:
node*nn=new node(d);
nn->left=BuildTree();
nn->right=BuildTree();
return nn;
}
hi @rohanmd07
i have seen just now that it is mention that input format is in inorder
Input Format
Level order input for the binary tree will be given.
so correct way to take input is
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;
}
}
to take input of unknown size array use can do
int arr[1000];
int x;
int i=0
while(cin>>x){
arr[i]=x;
i++;
}