Binary tree One of data structure experiments: isomorphism of treesTime limit:1000ms Memory limit:65536kb Submit Statisticproblem Description
Given two trees T1 and T2. If T1 can be converted to T2 by several times or so, we call the two trees "isomorphic". Example 1 gives the two trees is isomorphic, because we put one of the nodes of a tree, a, B, g of the children of the right and left after the exchange, we get another tree. and Figure 2 is not isomorphic.
Figure 1
Figure 2
Now given two trees, please judge whether they are isomorphic.
Input data contains multiple groups, each set of data given2 binary tree information. For each tree, first a non-negative integer N (≤ 10) is given in a row, that is, the node number of the tree (at this point the node is assumed to be numbered from 0 to n?1), and then N lines, the line I corresponds to the number I node, gives the node stored in the 1 English capital letters, the number of the left child node, and the number of the right child node. If the child's node is empty, the "-" is given in the appropriate location . The given data is separated by a single space.
Note: The title guarantees that the letters stored in each node are different. Output if the two trees are isomorphic, the outputs "Yes", otherwise output"No". Example Input
8A 1 2B 3 4C 5-d-e 6-g 7-f-H- -8g-4b 7 6F--A 5 1H-C 0-d--------------E 2-
Example Output
Yes
DQE:
Binary tree isomorphism, similar to the chemical isomers , the subject is simple, using the structure and the pointer array to accept the data, the O (n) time of the cyclic organization of data to establish a two-fork tree, and then the O (n) time cycle to find the only non-parent node as the root of the tree, Then the chemical isomers to determine whether two binary trees are isomorphic.
1#include <iostream>2#include <cstdio>3 4 using namespacestd;5 6 structTree7 {8 CharC,l,r;9Tree *pr,*lt,*RT;Ten }; OneTree *f[ A]; A -Tree *creat (intN) - { the inti; - for(i=0; i<n;i++) - { - if(f[i]->l!='-') + { -f[f[i]->l- -]->pr=F[i]; +f[i]->lt=f[f[i]->l- - ]; A } at if(f[i]->r!='-') - { -f[f[i]->r- -]->pr=F[i]; -f[i]->rt=f[f[i]->r- - ]; - } - } in for(i=0; i<n;i++) - { to if(f[i]->pr==NULL) + returnF[i]; - } the returnNULL; * } $ Panax Notoginseng BOOLCMP (Tree *r1,tree *R2) - { the if(r1==null&&r2==NULL) + return true; A Else if(r1==null| | r2==NULL) the return false; + if(r1->c!=r2->c) - return false; $ if(CMP (R1->LT,R2->LT) &&cmp (r1->rt,r2->rt)) | | (CMP (R1->LT,R2->RT) &&cmp (r1->rt,r2->LT)) ) $ return true; - return false; - } the - intMain ()Wuyi { theTree *r1,*R2; - intn,m; Wu while(SCANF ("%d", &n)! =EOF) - { About Chara[3],b[3],c[3]; $ inti; - for(i=0; i<n;i++) - { -f[i]=NewTree; Ascanf"%s%s%s", a,b,c); +f[i]->c=*A; thef[i]->l=*b; -f[i]->r=*C; $f[i]->pr=f[i]->lt=f[i]->rt=NULL; the } ther1=creat (n); the thescanf"%d",&m); - for(i=0; i<m;i++) in { thef[i]=NewTree; thescanf"%s%s%s", a,b,c); Aboutf[i]->c=*A; thef[i]->l=*b; thef[i]->r=*C; thef[i]->pr=f[i]->lt=f[i]->rt=NULL; + } -R2=creat (m); the Bayi if(CMP (R1,R2)) theprintf"yes\n"); the Else -printf"no\n"); - } the return 0; the } the the /*************************************************** - User Name: * * * the result:accepted the Take time:0ms the Take memory:156kb94 Submit time:2016-11-04 20:08:40 the ****************************************************/
Sdut 3340 Data structure Experiment two forks tree one: The isomorphism of the tree