文章
28
粉丝
226
获赞
53
访问
146.0k
一、先建立树再后序
#include <iostream>
using namespace std;
/*
先建树再后序。
*/
string pre,in;
int n;
struct node{
char value;
node *left;
node *right;
};
int findmid(int l, int r){
for(int i = 0;i < pre.size();i++){
for(int j = l;j <= r;j++){
if(pre[i] == in[j]){
return j;
}
}
}
}
node* creat(int l,int r){
node *T;
if(l > r){
T = NULL;
}else{
T = (struct node*)malloc(sizeof(node));
int mid = findmid(l, r);
T->value = in[mid];
T->left = creat(l, mid-1);
T->right = creat(mid+1, r);
}
return T;
}
void PostOrderTra(node *T){
if(T != NULL){
PostOrderTra(T->left);
PostOrderTra(T->right);
cout << T->value;
}
}
int main()
{
cin >> pre >> in;
node *T = (struct node *)malloc(sizeof(node));
T = creat(0, ...
登录后发布评论
暂无评论,来抢沙发