#include
#include
using namespace std;
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node *BuildTree(int *in, int *post, int s, int e)
{
static int i=e;
if(s>e)
{
return NULL;
}
struct node *root;
root=new node;
root->data=post[i];
int index=-1;
for(int j=s;j<=e;j++)
{
if(in[j]==post[i])
{
index=j;
break;
}
}
i–;
root->left=BuildTree(in, post, s, index-1);
root->right=BuildTree(in, post, index+1, e);
return root;
}
void bfs(struct node root)
{
queue<struct node>q;
q.push(root);
q.push(0);
while(!q.empty())
{
struct node* temp=q.front();
if(temp==0)
{
cout<<endl;
q.pop();
if(!q.empty())
{
q.push(0);
}
}
else
{
cout<data<<" ";
q.pop();
if(temp->left)
{
q.push(temp->left);
}
if(temp->right)
{
q.push(temp->right);
}
}
}
}
int main()
{
int post[]={5,17,19,18,16,70,85,60,20};//Left Right Root
int in[]= {5,16,17,18,19,20,60,70,85};//Left Root Right
int n=sizeof(in)/sizeof(in[0]);
struct node *root;
root=BuildTree(in, post, 0, n-1);
bfs(root);
// inorder(root);
return 0;
}
what’s wrong in this code I’ve written? Please Clear me!!