(二叉树建树 先序遍历)UVA_122 Trees on the level
来源:互联网 发布:网络播放器哪个平台好 编辑:程序博客网 时间:2024/06/05 17:16
UVA_122 Trees on the level
题意
让我们构建一棵树,给出每一个节点是从根节点怎么走才能走到.
比如(11,LL)就是根节点的左子树的左子节点的位置权值为11.然后让给出这棵树的先序遍历结果.
如果出现下列情况,输出”not complete”
- 重复赋值
- 没有根节点
- 有节点没有被赋值(子节点有赋值,但是本身却没有赋值的情况)
- 给出超过一个节点
解决
这个题的输入比较坑啊,刚上来感觉解决不了…
看了紫书,参考了博客.
解决输入
- 主函数中用一个大循环,一直循环创建一个bool readin()函数
- 当程序读入EOF的时候返回false,主函数退出循环
- 当readin中读入”()”,返回true,主函数继续循环
C语言字符串的灵活性-可以把任意”指向字符的指针”当做是字符串,从该位置开始,直到字符’\0’
建树
- 创建node结构体,包含指向左儿子和右儿子的指针
- 每次从根节点开始走,走到对应位置,对当前位置进行赋值
先序遍历
先序遍历=根节点+左儿子的先序+右儿子的先序
- 用一个vector来保存遍历结果
- 根节点入队
- 如果左儿子存在,左儿子入队
- 如果右儿子存在,右儿子入队
#include<bits/stdc++.h>using namespace std;#define de(x) cout << #x << "=" << x << endl#define rep(i,a,b) for(int i=a;i<(b);++i)struct node{ bool have_value; //是否已经被赋值 int v; node *left,*right; node():have_value(false),left(NULL),right(NULL){}};node *root;node *newnode(){return new node();} //new函数后面要加个括号bool gg;void addnode(int v,char *s){ int len=strlen(s); node *u=root; //u从根节点开始走,直到目标节点 rep(i,0,len){ if(s[i]=='L') { if(u->left==NULL) u->left=newnode(); u=u->left; continue; } if(s[i]=='R') { if(u->right==NULL) u->right=newnode(); u=u->right; continue; } } if(u->have_value){gg=true;return ;} //如果当前节点已经被赋值,gg u->v=v; u->have_value=true; //标记为已赋值}void delete_tree(node *u){ if(u==NULL) return ; delete_tree(u->left); delete_tree(u->right); delete u;}bool readin(){ char str[270]; gg=false; while(1) { if(scanf("%s",str)==EOF) return false; //程序读到EOF if(!strcmp(str,"()")) break; int v; //de(str); sscanf(&str[1],"%d",&v); //ssanf的使用要头文件sstream //de(v); addnode(v,strchr(str,',')+1); //strchr()返回字符串从左到右第一个字符‘,’的指针 } return true;}vector<int> ans;bool bfs(){ ans.clear(); queue<node*> Q; Q.push(root); while(!Q.empty()) { node *u=Q.front();Q.pop(); if(!u->have_value) {gg=true;return false;} ans.push_back(u->v); if(u->left!=NULL) Q.push(u->left); if(u->right!=NULL) Q.push(u->right); } return true;}int main(){ while(1) { delete_tree(root); root=newnode(); if(!readin()) break; //程序读到了EOF if(!gg&&bfs()) { for(int i=0;i<ans.size();i++) printf("%d%c",ans[i],i+1==ans.size()?'\n':' '); } else puts("not complete"); }}
阅读全文
0 0
- (二叉树建树 先序遍历)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 建立二叉树BFS层序遍历
- UVA 122 && HDU 1622 Trees on the level(构造二叉树+BFS层序遍历)
- hdu 1622 Trees on the level(二叉树的创建与遍历)
- UVa 122 Trees on the level(二叉树层次遍历)
- uva 122 Trees on the level 二叉树的层次遍历(bfs)
- UVA122 Trees on the level(二叉树建立与遍历)
- UVA 122 Trees on the level 二叉树层次遍历 数组&指针
- UVA 122 Trees on the level(指针二叉树,层次遍历)
- 例题6-7 二叉树的层次遍历 UVa 122 Trees on the level
- BST二叉搜索树的建树和先序遍历
- 华中科技-二叉树遍历1184,先序建树
- 二叉树先序建树及先序遍历
- 122 - Trees on the level[构建二叉树]
- UVA122 Trees on the level【二叉树】【BFS】
- JavaWeb学习之如何在 Serlvet 中获取请求信息
- Python 迭代器与生成器
- PHP文件保存出现的BOM头(utf-8)的解决方式——保存格式为ANSI即可
- c/c++整理--复制构造函数和深复制及浅复制
- Malmo——微软人工智能项目
- (二叉树建树 先序遍历)UVA_122 Trees on the level
- 设置Telnet 以及console 管理的账户密码
- google 高级搜索
- Spring_配置Bean
- ReactNative——Iamge的大学问
- 新手使用GreenDao
- PHP基础之数组2
- 机器学习——Python(四)
- 使用Gradle构建SpringBoot工程系列:第七篇:使用spring-boot-devtools开启热部署