Build a Tree using Postorder and Inorder Traversal

#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!!

Hi @sourav_817
please send the code in cb ide its diff to debug this way

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.