[UVA 122] Trees on the level 二叉树好难+BFS

来源:互联网 发布:证券软件下载 编辑:程序博客网 时间:2024/06/05 19:47

题目大意:
以(num,route)格式输入二叉树结点,()作为结束。
输出格式从上到下,从左到右,同一高度的输出。
所以还要用到bfs,注意队列的格式是指针


这里是用string转到char用sscanf搞来搞去格式一直错改了很久才改对的很长代码的笨办法。

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <queue>#include <vector>using namespace std;struct node{    int value;    bool havevalue;    node*left,*right;    node()    {        left=NULL;        right=NULL;        havevalue=false;    }};bool judge;vector <int> NodeQue;node*head;void addnode(int v,char*route){    node*insertno=head;    for(int i=0;i<strlen(route);i++)    {        if(route[i]=='L'){            if(insertno->left==NULL)                insertno->left=new node();            insertno=insertno->left;        }        else{            if(insertno->right==NULL)                insertno->right=new node();            insertno=insertno->right;        }    }    if(insertno->havevalue)judge=false;    insertno->value=v;    insertno->havevalue=true;}bool bfs(){    NodeQue.clear();    queue <node*> que;    que.push(head);    while(!que.empty())    {        node*Out=que.front();        que.pop();        if(!Out->havevalue)return false;        //cout<<Out->value<<' ';        NodeQue.push_back(Out->value);        if(Out->left!=NULL){            que.push(Out->left);        }        if(Out->right!=NULL){            que.push(Out->right);        }    }    return true;}int main(){    char input;    string OneNode;    char OneNodec[300];    judge=true;    head=new node();    while(scanf(" %c",&input)!=EOF)    {        OneNode+=input;        if(OneNode=="()"){            if(judge&&bfs()){                for(int i=0;i<NodeQue.size();i++){                    if(i)cout<<' ';                    cout<<NodeQue[i];                }cout<<endl;            }            else cout<<"not complete"<<endl;            OneNode="";            head=new node();            judge=true;            continue;        }        if(input==')')        {            OneNode=OneNode.substr(1,OneNode.length()-2);            //cout<<OneNode<<endl;            int v;            char c;            char route[300];            strcpy(OneNodec, OneNode.c_str());            if(OneNode[OneNode.length()-1]==','){                sscanf(OneNodec,"%d",&v);                addnode(v,"");            }            else{                sscanf(OneNodec,"%d%c%s",&v,&c,route);                addnode(v,route);            }            OneNode="";        }    }    return 0;}

在智慧的明明的启发下发现欸输入的()之间有空格!!!
所以可以全程scanf用char数组搞
哇!节省了20行!
主要是智障如我用惯了string函数不记得char的函数了

    #include <iostream>    #include <cstdio>    #include <cstring>    #include <string>    #include <queue>    #include <vector>    using namespace std;    struct node    {        int value;        bool havevalue;        node*left,*right;        node()        {            left=NULL;            right=NULL;            havevalue=false;        }    };    bool judge;    vector <int> NodeQue;    node*head;    void addnode(int v,char*route)    {        //cout<<route<<endl;        node*insertno=head;        for(int i=0;i<strlen(route)-1;i++)//注意传过来的route多一个')'        {            if(route[i]=='L'){                if(insertno->left==NULL)                    insertno->left=new node();                insertno=insertno->left;            }            else{                if(insertno->right==NULL)                    insertno->right=new node();                insertno=insertno->right;            }        }        if(insertno->havevalue)judge=false;        insertno->value=v;        insertno->havevalue=true;    }    bool bfs()    {        NodeQue.clear();        queue <node*> que;        que.push(head);        while(!que.empty())        {            node*Out=que.front();            que.pop();            if(!Out->havevalue)return false;            NodeQue.push_back(Out->value);            if(Out->left!=NULL){                que.push(Out->left);            }            if(Out->right!=NULL){                que.push(Out->right);            }        }        return true;    }    int main()    {        char input;        char OneNode[300];        judge=true;        head=new node();        while(scanf(" %s",&OneNode)!=EOF)        {            if(strcmp(OneNode,"()")==0){                if(judge&&bfs()){                    for(int i=0;i<NodeQue.size();i++){                        if(i)cout<<' ';                        cout<<NodeQue[i];                    }cout<<endl;                }                else cout<<"not complete"<<endl;                head=new node();                judge=true;                continue;            }                int v;                char c;                char route[300];                int len=strlen(OneNode);                    sscanf(&OneNode[1],"%d",&v);                    addnode(v,strchr(OneNode,',')+1);        }        return 0;    }

心情糟糕的时候果然是代码最容易让人平静了。
真情实感的追剧是会遭报应的。
真情实感的写代码就能写出来的。

原创粉丝点击