首页 > 代码库 > 灰哥的二叉树

灰哥的二叉树

//知道中序后序求先序
#include<iostream>#include<cstdio>#include<cstring>#include<string>using namespace std;const int N=10;int la,lb;char a[N],b[N];struct node{ int ls,rs; char c;}tree[N];int cnt=0;int build(int l,int r,int s,int t){ if(l>r||s>t) return 0; tree[++cnt].c=b[t]; int u=cnt; int root=0; for(root=l;root<=r;root++) if(a[root]==b[t]) break; tree[u].ls=build(l,root-1,s,s+root-l-1); tree[u].rs=build(root+1,r,s+root-l,t-1); return u;}void print(int u){ if(u==0) return; printf("%c",tree[u].c); print(tree[u].ls); print(tree[u].rs);}int main(){ scanf("%s%s",a+1,b+1); la=strlen(a+1);lb=strlen(b+1); build(1,la,1,lb); print(1); }

 

灰哥的二叉树