您的位置:首页 > 其它

PAT 是否为同一棵二叉搜索树

2016-07-30 11:52 337 查看
#include <iostream>

#include <string.h>

#include <cstring>

#include <cstdio>

#include <cstdlib>

using namespace std;

int n,l;

typedef struct dataStruct{

    dataStruct* left;

    dataStruct* right;

    int         data;

}*node;

void CreatTree(node &T, int dt){

    if(T == NULL){

        T  = new dataStruct();

        T->data = dt;

        T->left = NULL;

        T->right = NULL;

        return ;

    }

    if(T->data > dt){

        CreatTree(T->left, dt);

    }else{

        CreatTree(T->right, dt);

    }

}

bool isEqual(node t1,node t2){

    if(t1 != NULL && t2 != NULL){

        if(t1->data != t2->data){

            return false;

        }

        return (isEqual(t1->left,t2->left)&&isEqual(t1->right,t2->right));

    }else{

        if(t1 == NULL && t2 ==NULL){

            return true;

        }else{

            return false;

        }

    }

}

int main(){

    int w;

    int len1;

    node t1,t2;

    while(cin>>n && n != 0){

        cin>>l;

        t1 = NULL;

        for(int i = 0 ;i < n ;i++){

            cin>>w;

            CreatTree(t1,w);

        }

        for(int i =  0 ;i < l ;i++){

            t2 = NULL;

            for(int j = 0 ;j < n ;j++){

                cin>>w;

                CreatTree(t2, w);

            }

             if(isEqual(t1,t2)){

                    cout<<"Yes"<<endl;

            }else{

                    cout<<"No"<<endl;

            }

        }

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: