The sub-structure of an OFFER tree (9-degree OJ1520)

Source: Internet
Author: User

Description:

Enter Two Binary Trees A and B to determine whether B is A sub-structure.

 

Input:

The input may contain multiple test examples. The input ends with EOF.
For each test case, the input first line is an integer n, m (1 <= n <= 1000, 1 <= m <= ): n indicates the number of nodes of the Binary Tree A to be input (the number of nodes starts from 1), and m indicates the number of nodes of the Binary Tree B to be input (the number of nodes starts from 1 ). The next row contains n numbers, each representing the I-th element of the tree, and the next n rows. The first Ki number represents the number of children on the I-th node, next there are Ki trees, representing the node I child node number. The next m + 1 line is the same as the description of tree.

 

Output:

Corresponding to each test case,
If B is A, the subtree outputs "YES" (excluding quotation marks ). Otherwise, "NO" is output (excluding quotation marks ).

 

Sample input:
7 38 8 7 9 2 4 72 2 32 4 5002 6 7008 9 22 2 3001 12030

 

Sample output:
YESNO

 

Tip:
B is not a subtree of any tree.
Solution:

This question has a pitfall,First, the range of n and m must not be 0, but the third and fourth in the test case may be the first tree null and the second number empty.. Therefore, pay special attention to the limitations of mn in scanf ("% d", & n, & m.

In additionThe use case does not show a single leaf. Note that when the input is 1, there is only one node and it is the left subtree node.

For example, if there is only one child

  

Number of children on the left

 

In addition, it is the main idea of this question.First, we still use the structure tree used in the previous question. The main idea is to traverse the tree on the left and compare it with the tree on the right. If different, compare the child nodes on the left. Until all the trees are traversed.

During the comparison, determine whether the tree on the right is empty and whether the vertex on the left is empty. Once the comparison elements are different, the comparison fails.

The main code is to imitate the code in the book, and the total number of bugs written by myself is ah.

int testForCompare(TreeArr *t1,int t1i,TreeArr *t2){    int result = 0;    if(t1i != 0 && t2->arr[0].num != 0){        if(t1->arr[t1i].num == t2->arr[1].num)            result = compare(t1,t1i,t2,1);        if(!result)            result = testForCompare(t1,t1->arr[t1i].lchild,t2);        if(!result)            result = testForCompare(t1,t1->arr[t1i].rchild,t2);    }    return result;};int compare(TreeArr *t1,int t1i,TreeArr *t2,int t2i){    if(t2i == 0)        return 1;    if(t1i == 0)        return 0;    if(t1->arr[t1i].num != t2->arr[t2i].num)        return 0;    return compare(t1,t1->arr[t1i].lchild,t2,t2->arr[t2i].lchild)&&compare(t1,t1->arr[t1i].rchild,t2,t2->arr[t2i].rchild);}
All code:
# Include <stdio. h> # include <stdlib. h ># define MAXSIZE 1005 typedef struct treeelement {int num; int lchild; int rchild;} TreeElement; typedef struct treearr {TreeElement arr [MAXSIZE];} TreeArr; int testForCompare (TreeArr * t1, int t1i, TreeArr * t2); int compare (TreeArr * t1, int t1i, TreeArr * t2, int t2i); int main (void) {int n, m, I, nchild, n1, n2; TreeArr * t1 = (TreeArr *) malloc (sizeof (TreeArr); TreeArr * t2 = (Tre EArr *) malloc (sizeof (TreeArr); while (scanf ("% d", & n, & m )! = EOF ){//.... initialize the tree for (I = 0; I <1000; I ++) {t1-> arr [I]. num = 0; t1-> arr [I]. lchild = 0; t1-> arr [I]. rchild = 0; t2-> arr [I]. num = 0; t2-> arr [I]. lchild = 0; t2-> arr [I]. rchild = 0;} t1-> arr [0]. num = n; t2-> arr [0]. num = m ;//..... input the first tree for (I = 1; I <= n; I ++) {scanf ("% d", & t1-> arr [I]. num) ;}for (I = 1; I <= n; I ++) {scanf ("% d", & nchild); if (nchild = 2) {scanf ("% d", & n1, & n2); t1-> arr [I]. lc Hild = n1; t1-> arr [I]. rchild = n2;} else if (nchild = 1) {// not sure how to input it? The scanf ("% d", & n1); t1-> arr [I]. lchild = n1;} else if (nchild = 0 ){}}//........ input the second tree for (I = 1; I <= m; I ++) {scanf ("% d", & t2-> arr [I]. num) ;}for (I = 1; I <= m; I ++) {scanf ("% d", & nchild); if (nchild = 2) {scanf ("% d", & n1, & n2); t2-> arr [I]. lchild = n1; t2-> arr [I]. rchild = n2;} else if (nchild = 1) {// not sure how to input it? The scanf ("% d", & n1); t2-> arr [I]. lchild = n1;} else if (nchild = 0) {}} // testing for compare if (testForCompare (t1, 1, t2 )) printf ("YES \ n"); else printf ("NO \ n");} return 0 ;}; int testForCompare (TreeArr * t1, int t1i, TreeArr * t2) {int result = 0; if (t1i! = 0 & t2-> arr [0]. num! = 0) {if (t1-> arr [t1i]. num = t2-> arr [1]. num) result = compare (t1, t1i, t2, 1); if (! Result) result = testForCompare (t1, t1-> arr [t1i]. lchild, t2); if (! Result) result = testForCompare (t1, t1-> arr [t1i]. rchild, t2) ;}return result ;}; int compare (TreeArr * t1, int t1i, TreeArr * t2, int t2i) {if (t2i = 0) return 1; if (t1i = 0) return 0; if (t1-> arr [t1i]. num! = T2-> arr [t2i]. num) return 0; return compare (t1, t1-> arr [t1i]. lchild, t2, t2-> arr [t2i]. lchild) & compare (t1, t1-> arr [t1i]. rchild, t2, t2-> arr [t2i]. rchild );} /*************************************** * *********************** Problem: 1520 User: xhalo Language: C Result: Accepted Time: 10 MS Memory: 912 kb ************************************** **************************/

 

Related Article

Contact Us

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.