后序+中序序列构造二叉树(递归法)
post 为后序序列; in 为中序序列; len 为序列长度。
#include
#include
#include
#define N 1000
using namespace std;
struct node{
char data;
struct node * lchild;
struct node * rchild;
};
struct node * xx(char *post,char *in,int len);
void Preorder(struct node * root)
{
if(root)
{
cout<data;
Preorder(root->lchild);
Preorder(root->rchild);
}
}
int main()
{
int n,i;
char post[N];
char in[N];
cin>>n;
for(i=0;i>post[i];
for(i=0;i>in[i];
struct node *root=xx(post,in,n);
Preorder(root);
return 0;
}
/* 请在这里填写答案 */
输入样例:
第一行输入序列长度n,第二行输入n个字符表示二叉树后序遍历的序列,第三行输入n个字符表示二叉树中序遍历的序列
9
GHDBEIFCA
GDHBAECIF
输出样例:
输出二叉树先序遍历的序列。
ABDGHCEFI
递归方法我没有思路