本文共 1590 字,大约阅读时间需要 5 分钟。
#includeusing namespace std;struct A{ char ch; int l,r; bool root;}a[15],b[15];int n1,n2;char ch;char l,r;bool judge(int root1,int root2){ if(root1==-1&&root2==-1) return true; if((root1==-1&&root2!=-1)||(root1!=-1&&root2==-1)) return false; if(a[root1].ch!=b[root2].ch) return false; if(judge(a[root1].l,b[root2].l)&&judge(a[root1].r,b[root2].r)) return true; if(judge(a[root1].l,b[root2].r)&&judge(a[root1].r,b[root2].l)) return true; return false;}void print(int r){ if(r==-1) return; cout< <<' '; print(b[r].l); print(b[r].r);}void build(){ cin>>n1; for(int i=0;i >ch>>l>>r; a[i].ch=ch; if(l=='-') a[i].l=-1; else { a[i].l=l-'0'; a[a[i].l].root=true;//节点不是树根 //正常来说应该是false,但因为初始化是true //所以用true来否定,下同。 } if(r=='-') a[i].r=-1; else { a[i].r=r-'0'; a[a[i].r].root=true; } } cin>>n2; for(int i=0;i >ch>>l>>r; b[i].ch=ch; if(l=='-') b[i].l=-1; else { b[i].l=l-'0'; b[b[i].l].root=true; } if(r=='-') b[i].r=-1; else { b[i].r=r-'0'; b[b[i].r].root=true; } }}int main(){ //freopen("in.txt","r",stdin); build();//建树 int root1=-1,root2=-1; for(int i=0;i
ps:答案仅供参考,请勿抄袭
转载地址:http://nlqbi.baihongyu.com/