【二叉树】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 ...
随机推荐
- CNNs 在图像分割中应用简史: 从R-CNN到Mask R-CNN
作者:嫩芽33出处:http://www.cnblogs.com/nenya33/p/6756024.html 版权:本文版权归作者和博客园共有 转载:欢迎转载,但未经作者同意,必须保留此段声明:必须 ...
- spring (由Rod Johnson创建的一个开源框架)
你可能正在想“Spring不过是另外一个的framework”.当已经有许多开放源代码(和专有)J2EEframework时,我们为什么还需要Spring Framework? Spring是独特的, ...
- FIBON高精度
#include<stdio.h> #include<string.h> int u,n; ],b[],h[]; ],y[],z[]; int main() { char s( ...
- JS与JQ 获取页面元素值的方法和差异对比
获取浏览器高度和宽度 document.documentElement.clientWidth ==> 浏览器可见区域宽度 document.documentElement.clientHeig ...
- 树状数组 简单题 cf 961E
题目链接 : https://codeforces.com/problemset/problem/961/E One day Polycarp decided to rewatch his absol ...
- springboot-i18n国际化
简介 In computing, internationalization and localization are means of adapting computer software to di ...
- baidumap demo(三)
定位 您可以通过以下代码来开启定位功能: 源码复制打印关于 //开启定位功能 [_mapView setShowsUserLocation:YES]; 定位成功后,可以通过mapView.userLo ...
- bzoj5469 [FJOI2018]领导集团问题
题目描述: bz luogu 题解: 相当于树上$LIS$问题. 考虑一维情况下的贪心,我们可以用multiset启发式合并搞. 代码: #include<set> #include< ...
- (63)zabbix low-level discover zabbix批量部署必备
1. 概述 <zabbix发现配置>server通过配置好的规则,自动添加host.group.template <zabbix Active agent自动注册>与disco ...
- (42)zabbix使用IT services 了解服务器SLA整体情况
什么是IT Services 服务器或者某项服务.业务的可用率,不懂技术的上级领导会过问最近服务器可用率如何.所有api的状况怎么样? 通常一些技术人员会说负载怎么样,哪些cpu使用率怎么样,硬盘使用 ...