How to build a binary tree level order vise
example:-
1 --> level 1st (root node)
2, 3 --> level 2
4, 5, 6, 7 -->level 3
How to build binary tree level order?
@rajsaxena.personal, you can build a binary tree level order wise using queue data structure (iteratively) the algo is somewhat similar to level order print
refer this :-
void LevelWiseInsertion(node* &root) ///iteratively
{
cout<<"enter the root node value"<<endl;
int d;
cin>>d;
root=new node(d);
queue<node*> q;
q.push(root);
while(!q.empty())
{
node* parent=q.front();
q.pop();
int c1,c2;
cout<<"enter the children of "<<parent->data<<endl;
cin>>c1>>c2;
if(c1!=-1)
{
parent->left=new node(c1);
q.push(parent->left);
}
if(c2!=-1)
{
parent->right=new node(c2);
q.push(parent->right);
}
}
}
refer this for complete program : https://ide.codingblocks.com/s/242183
feel free to ask , in case of any doubt
mark your doubt as resolved if you got your answer