[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; }
心情糟糕的时候果然是代码最容易让人平静了。
真情实感的追剧是会遭报应的。
真情实感的写代码就能写出来的。
阅读全文
0 0
- [UVA 122] Trees on the level 二叉树好难+BFS
- 【日常学习】【指针二叉树+BFS】Uva - 122 Trees on the level题解
- UVa 122 - Trees on the level【二叉树初接触(BFS)】
- UVa 122 Trees on the level 建立二叉树BFS层序遍历
- UVA 122 && HDU 1622 Trees on the level(构造二叉树+BFS层序遍历)
- uva 122 Trees on the level 二叉树的层次遍历(bfs)
- UVA 122 Trees on the level 二叉树
- UVA 122 Trees on the level (二叉树层次遍历)
- UVA 122 Trees on the level【二叉树】
- UVA122 Trees on the level【二叉树】【BFS】
- UVa Problem 122 - Trees on the level
- UVA 122 - Trees on the level
- uva 122 - Trees on the level
- uva 122 Trees on the level
- UVA-122-Trees on the level
- UVA - 122 Trees on the level
- UVa 122 - Trees on the level
- UVa 122 - Trees on the level
- iptables介绍与实战
- Lua自定义面向对象
- linux命令之系统服务控制练习
- hash取模将大文件转成小文件,可排序,可求TopN
- 项目csdn图片地址
- [UVA 122] Trees on the level 二叉树好难+BFS
- java的volatile关键字之非线程安全
- Add two numbers
- PTA 7-22(排序) 模拟EXCEL排序(25 分)25分代码 结构体排序
- Linux 基础操作(七)————系统服务的控制
- Removed Interval HDU
- (四)系统虚拟化关键技术
- 设备树详解(链接很好,作者嵌入式Linux见解深刻)
- android基本架构