Search one of 3373 data structure experiment: binary sorting tree

Search one of 3373 data structure experiment: binary sorting tree

Time Limit: 400MS Memory Limit: 65536KB

Problem Description

A binary sorting tree can be uniquely determined for a given sequence. However, a given binary sorting tree can be obtained from a variety of different sequences. For example, inserting the initial empty binary sort tree according to the sequence {3,1,4} and {3,4,1} respectively, all of them get the same result. Your task book determines whether the input sequences can generate the same binary sorting tree.

Input

The input contains several sets of test data. The first row of each group of data gives two positive integers N (N < = 10) and L, which are the number of elements of the input sequence and the number of sequences to be compared. The second line gives N positive integers separated by spaces as the initial insertion sequence to generate a binary sort tree. Then L lines, each line gives N elements, belonging to L sequences to be checked.
For simplicity, we guarantee that each insertion sequence is a permutation of 1 to N. When N is read as 0, mark the end of input. Do not process this set of data.

Output

For each set of sequences to be checked, if the binary sort tree generated by it is the same as the binary sort tree generated by the initial sequence, then "Yes" will be output, otherwise "No" will be output.

Example Input

4 2
3 1 4 2
3 4 1 2
3 2 4 1
2 1
2 1
1 2
0

Example Output

Yes
No
No
#include <iostream>  
#include <string.h>  
using namespace std;  
  
typedef struct node{  
    int data;  
    struct node *lchild, *rchild;  
}*tree;  
  
int n, m, flag;  
tree create(int x, tree root){  
    if(!root){  
        root = new node;  
        root->data = x;  
        root->lchild = root->rchild = NULL;  
    }  
    else{  
        if(x < root->data)  
            root->lchild = create(x, root->lchild);  
        else  
            root->rchild = create(x, root->rchild);  
    }  
    return root;  
}  
  
void judge(tree root1, tree root2)
{  
    if(root1 && root2)
    {  
        if(root1->data != root2->data){  
            flag = 1;  
            return ;  
        }  
        judge(root1->lchild, root2->lchild);  
        judge(root1->rchild, root2->rchild);  
    }  
}  
  
int main(){  
    while(cin>>n&&n){  
        cin>>m;  
        int x;  
        struct node *root;  
        root = NULL;  
        for(int i = 1; i <= n; i++){  
            cin>>x;  
            root = create(x, root);  
        }  
        while(m--){  
            struct node *root2;  
            root2 = NULL;  
            for(int i = 1; i <= n; i++){  
                cin>>x;  
                root2 = create(x, root2);  
            }  
            flag = 0;  
            judge(root, root2);  
            if(flag)  
                cout<<"No"<<endl;  
            else  
                cout<<"Yes"<<endl;  
        }  
    }  
    return 0;  
}  

Posted by kbc1 on Fri, 01 May 2020 10:05:54 -0700