废话不多说 直接上代码
希望大神前来指正
#include<bits/stdc++.h>
using namespace std;
typedef struct node{
char data;
struct node *left;
struct node *right;
}Tree;
string a;
string b;
//根据前序序列和中序序列建树
Tree *qianzhong(int l1,int l2,int r1,int r2)//要将两个序列头和尾的数传进去
{
if(l1>l2)
{
return NULL;
}
Tree *root=new Tree();
root->data=a[l1];
int q=0;
while(root->data!=b[q])
{
q++;
}
int llen=q-r1;
root->left=qianzhong(l1+1,l1+llen,r1,r1+q-1);
root->right=qianzhong(l1+llen+1,l2,q+1,r2);
return root