OFFER wins the sub-tree of (nine degrees OJ1520)

Subject description:

Two binary inputs A, B, B is judged not substructure A's.

 

Input:

Input may contain multiple test samples, enter to EOF end.
For each test case, a first line of the input integer n, m (1 <= n <= 1000,1 <= m <= 1000): n represents the number of nodes to be input binary tree A (node 1 starts from count), m represents a number to be input binary tree node B (node 1 from start counting). Next there is a row number n, the number of each of the representative values A i-th element of the tree, followed by n-rows, the number of sub Ki represents the number of children i of the first node, the next tree has a Ki , on behalf of the child node node i sub-labels. The next line m + 1, and A description of the same tree.

 

Output:

Corresponding to each test case,
if the output of the A B subtree of "YES" (without the quotes). Otherwise, output "NO" (without the quotes).

 

Sample input:
7 3
8 8 7 9 2 4 7
2 2 3
2 4 5
0
0
2 6 7
0
0
8 9 2
2 2 3
0
0

1 1
2
0
3
0

 

Sample output:
YES
NO

 

prompt:
B为空树时不是任何树的子树。

Problem-solving ideas:

  This question has a pit, first subject of the request range of n to m is not 0, but the third and fourth test case and might be a first space and a second number of empty tree special case . Therefore, special attention here, in scanf ( "% d% d" , & n, & m) when limited attention span of mn.

  Further use cases do not give the case of a single leaf, then noted that when the input is 1, only one node, and the subtree is the left node.

  For example, is entered when only one child

  

孩子节点的数目 左边孩子的编号

 

  The other is the main idea of the subject. First, we used the last time the subject is still using the tree structure, the main idea is to traverse left The tree did not elements, compared with a tree on the right. If not, then compare it to the left child node. Until complete traversal of all the trees.

  When making the comparison to determine whether the right of the tree is empty, and determining whether the apex of the left is empty, if it is found to compare different elements, as evidenced by the comparison fails.

  The main code, the code book to imitate, a total write their own BUG, ​​hey.

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 = (TreeArr *)malloc(sizeof(TreeArr));
    while(scanf("%d %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 %d",&n1,&n2);
                t1->arr[i].lchild = n1;
                t1->arr[i].rchild = n2;
            }else if(nchild == 1){//不确定是怎么输入的?样例中没有这项
                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 %d",&n1,&n2);
                t2->arr[i].lchild = n1;
                t2->arr[i].rchild = n2;
            }else if(nchild == 1){//不确定是怎么输入的?样例中没有这项
                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
****************************************************************/

 

Reproduced in: https: //my.oschina.net/u/204616/blog/545158

Guess you like

Origin blog.csdn.net/weixin_33736048/article/details/91989439