从点对中构建二叉树,并查找两个节点所对应的最小公共祖先

来源:互联网 发布:淘宝改评价怎么说 编辑:程序博客网 时间:2024/06/11 20:46
/*
input:
213 12 8 131 2 1 3 2 4 3 5 3 6 4 7 7 12 5 9 5 8 6 11 6 10 11 1310 9 2 101 2 1 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
*/
#include<vector>
#include<iostream>#include<stdlib.h>#include<fstream>#include<sstream>using namespace std;int number;struct TreeNode{int val;TreeNode* left;TreeNode* right;};TreeNode* find(TreeNode *root,int temp1){if(root==NULL||root->val==temp1){return root;}TreeNode* left=find(root->left,temp1);TreeNode* right=find(root->right,temp1);if(left)return left;else if(right)return right;elsereturn NULL;}void insert(TreeNode* a, TreeNode* b){if(a==NULL|b==NULL){//cout<<"a is "<<a<<" , b is "<<b<<" ";//cout<<"insert ERROR"<<endl; ;return;}if(a->left==NULL)a->left=b;else if(a->right==NULL)a->right=b;else{cout<<"a->val is "<<a->val<<"  , b val is "<<b->val<<" "<<" ,a->left is "<<a->left<<" ,a->right is "<<a->right<<" ";cout<<"insert ERROE"<<endl;}}void merge(TreeNode* a, TreeNode* b){if((a->left&&a->right)||b==NULL){cout<<"ERROR merge"<<endl;return;}insert(a,b->left);insert(a,b->right);delete b;}voidpre_order(TreeNode *root){if(root==NULL){return ;}number++;//cout<<"-*-"<<root->val<<"";pre_order(root->left);pre_order(root->right);}TreeNode*ancient(TreeNode* root,int a,int b){if(root==NULL||root->val==a||root->val==b){return root;}TreeNode* left=ancient(root->left,a,b);TreeNode* right=ancient(root->right,a,b);if(left&&right)return root;else{TreeNode*temp=NULL;if(left==NULL)temp=right;elsetemp=left;return temp;}}TreeNode* create(int temp1, int temp2){TreeNode* root=new TreeNode;root->val=temp1;root->left=NULL;root->right=NULL;root->left=new TreeNode;root->left->val=temp2;root->left->left=NULL;root->left->right=NULL;return root;}int main(int argc, char** argv){int test_case;int T;//cin>>T;/*   Read each test case from standard input.*/    ifstream fAssociation;    fAssociation.open("/home/archer/Workspace/CodeBlocks/2017/Code_training/1- sample_input.txt");    int number_input=0;    string s;    getline(fAssociation,s);    stringstream ss;    ss<<s;    ss>>number_input;    number_input=T;    int times=0;    while(!fAssociation.eof()&×<10)    {        times++;number=0;TreeNode* root;vector<TreeNode*> All;        string s;        int num[4];        getline(fAssociation,s);        if(!s.empty())        {            stringstream ss;            ss << s;            ss >> num[0];            ss >> num[1];            ss >> num[2];            ss >> num[3];//            cout<<"点的对数: "<<num[0]<<" "<<num[1]<<" "<<num[2]<<" "<<num[3]<<endl;        }int v,e,a,b;v=num[0];e=num[1];a=num[2];b=num[3];//cin>>v>>e>>a>>b;//cout<<"v is "<<v<<",e is "<<e<<" ,a is "<<" , b is "<<b<<endl;int temp1,temp2;int pig=0;        getline(fAssociation,s);        int pairs[2];        if(!s.empty())        {            stringstream ss;            ss<<s;        /*        */            for(int i=0;i<e;i++)            {//                cin>>temp1;//                cin>>temp2;                ss >> pairs[0];                ss >> pairs[1];                temp1=pairs[0];                temp2=pairs[1];                if(i==0)                {                    root=create(temp1,temp2);                    All.push_back(root);                }                else                {                    TreeNode* link=NULL;                    for(int j=0; j<All.size(); j++)                    {                        link=find(All[j],temp1);                        if(link)                        {                            pig++;                            if(link->left&&link->right)                            {                                cout<<" i is "<<i<<" , j is "<<j<<" .pig is "<<pig<<" ,[val is "<<link->val<<" ,link->left val is "<<link->left->val<<"  ,link->right val is "<<link->right->val<<" ],temp1 is "<<temp1<<" ,temp2 is "<<temp2<<endl;                                break;    //cout<<"link->left address is "<<link->left<<"  ,link->right address is "<<link->right<<" ,link->left val is "<<link->left->val<<"  ,link->right val is "<<link->right->val<<" ,val is "<<link->val<<" ,temp1 is "<<temp1<<" ,temp2 is "<<temp2<<", pig is "<<pig<<endl;                            }                            TreeNode* temp=new TreeNode;                            temp->val=temp2;                            temp->left=NULL;                            temp->right=NULL;                            insert(link,temp);                            break;                        }                    }    //cout<<"root is "<<root->val<<" ,find val "<<link->val<<" to "<<temp2<<" , link->left address is "<<link->left<<"  ,link->right address is "<<link->right<<endl;                    if(link==NULL)                    {                        root=create(temp1,temp2);                        All.push_back(root);    //cout<<"Link is NULL "<<" from "<<temp1<<" to "<<temp2<<endl;                    }                }            }        }//cout<<"All size is "<<All.size()<<" ,find ancient"<<endl;//cout<<"Initialize!"<<endl;int limitation=All.size();while(limitation--){int sum=0;for(int i=0; i<All.size(); i++){TreeNode* link=NULL;for(int j=0; j<All.size(); j++){if(i!=j&&All[i]&&All[j]){link=find(All[i],All[j]->val);if(link){merge(link,All[j]);All[j]=NULL;break;}}}}for(int i=0; i<All.size(); i++){if(All[i]){sum++;root=All[i];}}if(sum==1)break;}number=0;TreeNode* cp=ancient(root,a,b);pre_order(cp);//cout<<"ancient val  is "<<cp->val<<" ,number is "<<number<<endl;cout << "#" << times <<" "<<cp->val<<" "<<number<<endl;}return 0;//Your program should return 0 on normal termination.}

原创粉丝点击