【二叉树】hdu 1622 Trees on the level
【题意】
给定一棵树每个结点的权重和路径(路径用LR串表示),输出这棵树的层次遍历
【思路】
注意输入输出,sscanf用来格式化地截取需要的数据,strchr来在字符串中查找字符的位置
【Accepted】
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std; int num;
const int maxn=;
char str[maxn];
struct node{
int num;
node *lef;
node *rig;
};
node *root;
bool tag;
void bfs(node *rt){
queue<node *> Q;
Q.push(rt);
while(!Q.empty()){
node *q=Q.front();
Q.pop();
if(q==rt){
printf("%d",q->num);
}else{
printf(" %d",q->num);
}
if(q->lef){
Q.push(q->lef);
}
if(q->rig){
Q.push(q->rig);
}
free(q);
}
}
bool isComplete(node *rt){
queue<node *> Q;
Q.push(rt);
while(!Q.empty()){
node *q=Q.front();
Q.pop();
if(q->num==-){
return false;
}
if(q->lef!=NULL){
Q.push(q->lef);
}
if(q->rig!=NULL){
Q.push(q->rig);
}
}
return true;
}
void print(node *root){
if(tag==false){
printf("not complete\n");
return;
}
bool flag=isComplete(root);
if(!flag){
printf("not complete\n");
return;
}
bfs(root);
puts("");
}
int main(){
root=(node *)malloc(sizeof(node));
root->num=-;
root->lef=NULL;
root->rig=NULL;
tag=true;
while(scanf("%s",str)!=EOF){
if(!strcmp(str,"()")){
print(root);
root=(node *)malloc(sizeof(node));
root->num=-;
root->lef=NULL;
root->rig=NULL;
tag=true;
}else{
sscanf(&str[],"%d",&num);
// printf("%d\n",num);
char *comma=strchr(str,',');
char *path=comma+;
node *now=root;
for(char *i=path;*i!=')';i++){
if(*i=='L'){
if(now->lef==NULL){
node *nd=(node *)malloc(sizeof(node));
nd->num=-;
nd->lef=NULL;
nd->rig=NULL;
now->lef=nd;
}
now=now->lef;
}else{
if(now->rig==NULL){
node *nd=(node *)malloc(sizeof(node));
nd->num=-;
nd->lef=NULL;
nd->rig=NULL;
now->rig=nd;
}
now=now->rig;
}
}
if(now->num!=-){
tag=false;
}else{
now->num=num;
}
}
}
free(root);
return ;
}
【二叉树】hdu 1622 Trees on the level的更多相关文章
- hdu 1622 Trees on the level(二叉树的层次遍历)
题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS ( ...
- hdu 1622 Trees on the level
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1622 小白书上的题... #include<algorithm> #include< ...
- [ An Ac a Day ^_^ ] hdu 1662 Trees on the level 数据结构 二叉树
紫书上的原题 正好学数据结构拿出来做一下 不知道为什么bfs的队列一定要数组模拟…… 还可以练习一下sscanf…… #include<stdio.h> #include<iostr ...
- Trees on the level(指针法和非指针法构造二叉树)
Trees on the level Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- UVA.122 Trees on the level(二叉树 BFS)
UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...
- Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。
Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...
- UVA 122 -- Trees on the level (二叉树 BFS)
Trees on the level UVA - 122 解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...
- E - Trees on the level
Trees on the level Background Trees are fundamental in many branches of computer science. Current ...
- UVa 122 Trees on the level(二叉树层序遍历)
Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...
随机推荐
- 企业CIO、CTO必读的34个经典故事
一. 用人之道 去过庙的人都知道,一进庙门,首先是弥陀佛,笑脸迎客,而在他的北面,则是黑口黑脸的韦陀.但相传在很久以前,他们并不在同一个庙里,而是分别掌管不同的庙.弥乐佛热情快乐,所以来的人非常多,但 ...
- MySQL流程控制和存储过程介绍
/*定义变量方式1:set @变量名=值;方式2:select 值 into @变量名;方式3:declare 变量名 类型(字符串类型加范围) default 值; in参数 入参的值会仅在存储过程 ...
- 最新深度ghost win7系统下载
深度技术ghost win7系统 64位快速安装版 V2016年2月,深度技术ghost win7 64位快速安装版在不影响大多数软件和硬件运行的前提下,已经尽可能关闭非必要服务,自动安装AMD/In ...
- SQLITE-更新查询
SQLite -更新查询 SQLite UPDATE查询用于修改现有表中的记录.您可以使用WHERE子句与更新查询更新选中的行,否则会被更新的所有行. 语法: UPDATE查询的WHERE子句的基本语 ...
- SQLite - WHERE子句
SQLite - WHERE子句 SQLite WHERE子句用于指定一个条件同时抓取数据从一个表或多个表. 如果给定的条件满意,意味着true,然后从表中返回特定值.你会使用WHERE子句来筛选记录 ...
- fluent_python2
字典和集合 泛映射类型, 继承自collections.abc, Mapping和MutableMapping 标准库里的所有映射类型都是利用 dict 来实现的,因此它们有个共同的限制,即只有可散列 ...
- spark简单入门
本文由cmd markdown编辑,原始链接:https://www.zybuluo.com/jewes/note/35032 RDD是什么? RDD是Spark中的抽象数据结构类型,任何数据在Spa ...
- 把apk文件拖到re-sign.jar运行打开的界面找不到指定文件
下载一个zipalign.exe放到tools目录下面就可以了 点击下载
- javascript querySelector和getElementById通过id获取元素的区别
querySelector和getElementById通过id获取元素的区别 <!DOCTYPE html> <html> <head> <meta cha ...
- shell脚本,awk 根据文件某列去重并且统计该列频次。
a文件为 a a a s s d .怎么把a文件变为 a s d .怎么把a文件变为 a a a s s d 解题方法如下: 解题思路 [root@localhost study]# awk 'NR= ...