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

xiaoxiao2021-02-28  33

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

Time Limit: 400MS Memory Limit: 65536KB

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; }