3373=数据结构实验之查找一:二叉排序树

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 struct node
 5 {
 6     int data;
 7     struct node *left,*right;
 8 };
 9 struct node * new()
10 {
11     struct node *root;
12     root=(struct node*)malloc(sizeof(struct node));
13     root->left=NULL;
14     root->right=NULL;
15     return root;
16 };
17 struct node * built(struct node* root,int t)
18 {
19     if(root==NULL)
20     {
21         root=new();
22         root->data=t;
23         return root;
24     }
25     if(t>root->data)
26     {
27         root->right=built(root->right,t);
28         return root;
29     }
30     if(t<root->data)
31     {
32         root->left=built(root->left,t);
33         return root;
34     }
35 }
36 int judge(struct node*root1,struct node*root2)
37 {
38     if(root1==NULL&&root2==NULL)return 1;
39     if(root1!=NULL&&root2!=NULL)
40     {
41         if(root1->data==root2->data&&judge(root1->left,root2->left)==1&&judge(root1->right,root2->right))
42         {
43             return 1;
44         }
45         else return 0;
46     }
47     else return 0;
48 }
49 int main()
50 {
51     int n,m,i,t;
52     while(~scanf("%d",&n))
53     {
54         if(n==0)break;
55         scanf("%d",&m);
56         struct node *root1;
57         root1=NULL;
58         for(i=0; i<n; i++)
59         {
60             scanf("%d",&t);
61             root1=built(root1,t);
62         }
63         while(m--)
64         {
65             struct node *root2;
66             root2=NULL;
67             for(i=0; i<n; i++)
68             {
69                 scanf("%d",&t);
70                 root2=built(root2,t);
71             }
72             if(judge(root1,root2)==1)printf("Yes\n");
73             else printf("No\n");
74         }
75     }
76     return 0;
77 }

猜你喜欢

转载自www.cnblogs.com/Angfe/p/11878429.html