中序序列可以与先序,后序,层序序列中的任何一个建立一棵树,而后三者之间两两不能建树(因为无法区分根节点的左右子树)
按先序遍历的顺序来建立树,建树过程类似于斐波那契数列的求项过程
先建立第一层的根节点,接着就按步骤的顺序来建立节点,如下图
上代码
#include <iostream>
using namespace std;
struct node
{
int x;
node* lson;
node* rson;
};
int pre[1000],in[1000];
///二叉树的先序区间(prel,prer),中序区间(inl ,inr)
///通过先序区间找出根节点,可将中序区间分为左右子树,然后将左右子树看成单独的树
node* recreat(int prel,int prer,int inl, int inr)
{
if(prel>prer) return NULL;///若先序区间长度<=0,则返回空,即没有子树
node *root=new node;///建立根节点,并为其开辟空间
root->x=pre[prel];///给根节点赋值
int k;
for(k=inl;k<inr;k++)///中序区间中找出根节点,这样就可以将先序序列分为左右子树
{
if(in[k]==pre[prel])///pre[prel]是根节点的值,为了将中序序列分为左右子树
break;
}
int num=k-inl;///左子树的节点数目
///左子树的先序区间(pre+1,pre+1+num-1),中序区间(inl,k-1)
root->lson=recreat(prel+1,prel+1+num-1,inl,k-1);///返回左子树的根节点root,赋给根节点的左儿子
///右子树的先序区间(pre+num+1,prer),中序区间(k+1,inr)
root->rson=recreat(prel+num+1,prer,k+1,inr);///返回右子树的根节点root,赋给根节点的右儿子
return root;
}
void post(node* Node)
{
if(Node==NULL) return;
post(Node->lson);
post(Node->rson);
cout<<Node->x<<" ";
}
int main()
{
int n;cin>>n;
for(int i=0;i<n;i++) cin>>pre[i];
for(int i=0;i<n;i++) cin>>in[i];
node* root=recreat(0,n-1,0,n-1);///注意区间是(0,n-1),不是(0,n);否则会多0出来
post(root);
return 0;
}