#include <bits/stdc++.h>
#define TElemType char
using namespace std;
const int maxn = 1000;
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
char pre[maxn], in[maxn];
void postOrderTraverse(BiTree T){
if(T == NULL)
return;
postOrderTraverse(T->lchild);
postOrderTraverse(T->rchild);
printf("%c", T->data);
}
BiTree build(char *pre0, char *in0, int rt, int l, int r){
if(l > r){
return NULL;
}
BiTree root = (BiTree)malloc(sizeof(BiTNode));
root->data = pre0[rt];
int mid;
for(int i = l;i <= r;i++){
if(pre0[rt] == in0[i]){
mid = i;
break;
}
}
root->lchild = build(pre0, in0, rt+1, l, mid-1);
root->rchild = build(pre0, in0, rt+mid-l+1, mid+1, r);
return root;
}
int main()
{
cin>>pre>>in;
int num = strlen(pre);
BiTree root = build(pre, in, 0, 0, num-1);
postOrderTraverse(root);
return 0;
}
[SDUTOJ][3170]——二叉树——B数据结构上机测试4.1二叉树的遍历与应用1
最新推荐文章于 2023-02-03 14:36:37 发布