# 03-Tree 1 isomorphism of trees

Source: Internet
Author: User

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 format:

Enter information for 2 binary tree trees. For each tree, a non-negative integer is given first in a rowNN\le 10≤10), which is the node number of the tree (this assumes that the node is from 0 toN-1n −1 number), then n< span class= "Strut" > n Rows, section ii line corresponding number first ii nodes, gives the 1 English capital letters stored in the node, the number of their left child nodes, and the number of right child nodes. 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 format:

If the two trees are isomorphic, output "Yes", otherwise output "No".

Input sample 1 (corresponding to Figure 1):
``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 -``
Output Example 1:
``Yes``
Input sample 2 (corresponding to Figure 2):
``8B 5 7F - -A 0 3C 6 -H - -D - -G 4 -E 1 -8D 6 -B 5 -E - -H - -C 0 2G - 3F - -A 1 4``
Output Example 2:
``No``
`1#include <stdio.h>2 3 #defineOK 14 #defineERROR 05 6 #defineMaxtree 107 #defineNull-1//NULL 0 different from the system8 9typedefintStatus;/*Status is the type of the function, whose value is the function result status code, such as OK, etc.*/TentypedefCharElementType;/*the Elemtype type is based on the actual situation and is assumed to be char*/ One  AtypedefstructTreeNode - { - ElementType data; the     intLeft ; -     intRight ; - } Tree; -  + Tree T1[maxtree], t2[maxtree]; -   + intBulidtree (Tree t[]) A { at     intN, check[maxtree], root = Null;//root = null empty tree returns null -     CharCL, CR;//left and right child serial number -scanf"%d\n",&N); -     if(N) { -          for(inti =0; i < N; i++) -Check[i] =0; in          for(inti =0; i < N; i++) { -scanf"%c%c%c\n",&t[i].data,&cl,&CR); to             //Find Root +             if(CL! ='-') { -T[i].left = CL-'0'; theCheck[t[i].left] =1;//not a root node *}Else { \$T[i].left =Null;Panax Notoginseng             } -             if(CR! ='-') { theT[i].right = CR-'0'; +Check[t[i].right] =1;//not a root node A}Else { theT[i].right =Null; +             } -         } \$          \$          for(inti =0; i < N; i++)//check[]=0 is the root node -             if(!Check[i]) {              -Root =i; the                  Break; -             }Wuyi     } the     returnRoot; - } WuStatus Isomprphic (intROOT1,intRoot2) - { About     if((ROOT1 = = null) && (ROOT2 = = null))//all are empty, isomorphic \$         returnOK; -     if((ROOT1 = = null) && (ROOT2! = null) | | (ROOT1! = null) && (ROOT2 = = null))//One of them is empty, and a different structure -         returnERROR; -     if(T1[root1].data! = t2[root2].data)//Unlike root data, different A         returnERROR; +     if((T1[root1].left = = null) && (t2[root2].left = = null))//left dial hand tree is empty, then the right subtree is judged the         returnisomprphic (T1[root1].right, t2[root2].right); -          \$     if((T1[root1].left! = null) && (t2[root2].left! = null) && the(T1[t1[root1].left].data = = T2[t2[root2].left].data))//Two tree left subtree is not empty, and the value is equal the         return(Isomprphic (T1[root1].left, T2[root2].left) &&//judging the subtree the isomprphic (T1[root1].right, T2[root2].right)); the     Else //Two tree left subtree is empty or not empty, but the value is not equal -         return(Isomprphic (T1[root1].left, T2[root2].right) &&//exchange left and right subtree judgment in isomprphic (T1[root1].right, T2[root2].left)); the      the } About  the intMain () the { the     introot1, Root2; +ROOT1 =Bulidtree (T1); -Root2 =Bulidtree (T2); the     if(Isomprphic (ROOT1, Root2))Bayiprintf"yes\n"); the     Else theprintf"no\n"); -     return 0; -}`

03-Tree 1 isomorphism of trees

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.