Address: HDU 1710
It is known that Binary Trees are sorted in ascending order and in descending order.
#include <stdio.h>#include <string.h>int a[1001], cnt;typedef struct node{ int date ; node *lchild , *rchild ;}*tree;int getk(int ch,int ino[],int is,int n){ for(int i = is ; i <= is + n -1 ; i++) if(ino[i]==ch)return i;}void gettree(tree &t,int pre[],int ino[],int ps,int is,int n){ if(n==0) t = NULL; else { t = new node; t->date = pre[ps]; int k = getk(pre[ps],ino,is,n); if(k == is) t->lchild = NULL; else gettree(t->lchild,pre,ino,ps+1,is,k-is); if(k == is+n-1) t->rchild = NULL; else gettree(t->rchild,pre,ino,ps+1+(k-is),k+1,n-1-(k-is)); }}void pritree1(tree t){ if(t) { pritree1(t->lchild); pritree1(t->rchild); a[cnt++]=t->date; }}int main(){ int i, n, t; int pre[1001] , ino[1001] ; while(scanf("%d", &n)!=EOF) { cnt=0; tree head; for(i=0; i<n; i++) { scanf("%d",&pre[i]); } for(i=0; i<n; i++) { scanf("%d",&ino[i]); } gettree(head,pre,ino,0,0,n); pritree1(head); for(i=0; i<cnt-1; i++) printf("%d ",a[i]); printf("%d\n",a[cnt-1]); } return 0;}