Trees on the level

【代码超详解】UVA 122 Trees on the Level(二叉树、BFS) 紫书 第六章 例题 6-7 Trees on the level, Duke 1993, UVa 122 第一回手写二叉树,今天就为这道题磕了一整天才过了样例,不过好在一提交就AC了。 一、题目描述 输入包含多组数据。每组数据给出一系列有序对 (v, s) (单个空格隔开两个对)代表二叉树的某个节点的值,你的任务是从根到叶输出各个节点的值 v 。同一层输出顺序自然是从左到右。最多有 256 个节点... 阅读详情

题目:

Trees are fundamental in many branches of computer science. Current state-of-the art parallel computers such as Thinking Machines' CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many algorithms in computer graphics. 

This problem involves building and traversing binary trees. 
Given a sequence of binary trees, you are to write a program that prints a level-order traversal of each tree. In this problem each node of a binary tree contains a positive integer and all binary trees have have fewer than 256 nodes. 

In a level-order traversal of a tree, the data in all nodes at a given level are printed in left-to-right order and all nodes at level k are printed before all nodes at level k+1. 

For example, a level order traversal of the tree 

 
is: 5, 4, 8, 11, 13, 4, 7, 2, 1. 

In this problem a binary tree is specified by a sequence of pairs (n,s) where n is the value at the node whose path from the root is given by the string s. A path is given be a sequence of L's and R's where L indicates a left branch and R indicates a right branch. In the tree diagrammed above, the node containing 13 is specified by (13,RL), and the node containing 2 is specified by (2,LLR). The root node is specified by (5,) where the empty string indicates the path from the root to itself. A binary tree is considered to be completely specified if every node on all root-to-node paths in the tree is given a value exactly once. 
 

Input

The input is a sequence of binary trees specified as described above. Each tree in a sequence consists of several pairs (n,s) as described above separated by whitespace. The last entry in each tree is (). No whitespace appears between left and right parentheses. 

All nodes contain a positive integer. Every tree in the input will consist of at least one node and no more than 256 nodes. Input is terminated by end-of-file. 
 

Output

For each completely specified binary tree in the input file, the level order traversal of that tree should be printed. If a tree is not completely specified, i.e., some node in the tree is NOT given a value or a node is given a value more than once, then the string ``not complete'' should be printed

Sample Input

(11,LL) (7,LLL) (8,R)
(5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
(3,L) (4,R) ()

Sample Output

5 4 8 11 13 4 7 2 1
not complete

 

题意:

输入一颗二叉树,你的任务是按照从上到下,从左到右的顺序输出各个结点的值。每个结点都按照从根结点到他的移动顺序给出(L表示左,R表示右)。在输入中,每个结点的左括号和右括号之间没有空格,相邻结点之间用一个空格隔开。每棵树的输入用一对空括号()结束。如果不能建成树输出 not complete;

 

思路:

建一棵树,然后把每个节点的路径保存到set容器中,保证路径不能相同,之后利用深搜,从字符串为空,每次添加L或R检查新的字符串是否存在,如果存在sum+1,如果最后sum == t(输入的节点数)则说明能构成树。

 

代码:

#include<stdio.h>
#include<set>
#include<string>
#include<string.h>
#include<algorithm>
using namespace std;
int t,fff,sum;
set<string>h;
struct node
{
    int a,l;
    char ss[300];
} s[300];
bool cmp(node x,node y)
{
    if(x.l==y.l)return strcmp(x.ss,y.ss)<0;
    else return x.l<y.l;
}
void dfs(int a,string as)
{
    int i=a;
    while(i<t)
    {
        if(as.length()+1<strlen(s[i].ss))break;
        string ac=as+'L';
        if(ac.compare(s[i].ss)==0)
        {
            sum++;
            dfs(i+1,ac);
        }
        ac=as+'R';
        if(ac.compare(s[i].ss)==0)
        {
            sum++;
            dfs(i+1,ac);
            break;
        }
        i++;
    }
}
int main()
{
    char k[300];
    while(~scanf("%s",k))
    {
        h.clear();
        t=0;
        fff=0;
        int flag=0;
        int gen=-0x3f3f3f3f;
        if(strlen(k)==2)
        {
            printf("not complete\n");
            continue;
        }
        else do
            {
                int l=strlen(k);
                if(l==2)
                    break;
                int num=0;
                int i=1,f=0,bl=0;
                char b[300];
                for(int j=0; j<300; j++)b[j]='\0';
                while(k[i]<='9'&&k[i]>='0')
                {
                    num=num*10+k[i]-'0';
                    i++;
                }
                i++;
                while(k[i]=='L'||k[i]=='R')
                {
                    f=1;
                    b[bl++]=k[i];
                    i++;
                }
                if(f==0)
                {
                    if(flag==0)
                    {
                        gen=num;
                        flag=1;
                    }
                    else fff=1;
                }
                else
                {
                    if(h.find(b)!=h.end())
                        fff=1;
                    else
                    {
                        h.insert(b);
                        s[t].a=num;
                        strcpy(s[t].ss,b);
                        s[t++].l=strlen(b);
                    }
                }
            }
            while(scanf("%s",k));
        sort(s,s+t,cmp);
        if(gen==-0x3f3f3f3f)fff=1;
        if(fff==1)printf("not complete\n");
        else
        {
            sum=0;
            dfs(0,"");
            if(sum!=t)fff=1;
            if(fff==1)printf("not complete\n");
            else
            {
                printf("%d",gen);
                for(int i=0; i<t; i++)
                    printf(" %d",s[i].a);
                printf("\n");
            }
        }
    }
    return 0;
}

 

『杭电1622』Trees on the level Problem Description Trees are fundamental in many branches of computer science. Current state-of-the art parallel computers such as Thinking Machines' CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many algorithms in computer graph 阅读详情

相关推荐

Grafana企业版vs开源版:从安装到插件管理的完整对比实测(含离线部署技巧)

本文深入对比Grafana企业版与开源版在安装流程、插件管理和功能限制等方面的差异,特别针对离线部署场景提供实用技巧。通过实测数据源支持、权限管理等核心功能,帮助企业用户做出明智选择,同时详细介绍离线安装和插件签名验证等关键操作。

weixin_29313817的博客 102

Trees on the level (二叉搜索树的还原)

Trees are fundamental in many branches of computer science. Current state-of-the art parallel computers such as Thinking Machines' CM-5 are based on fat trees. Quad- and octal-trees are fundamental to...

自渡的博客 714

全网首发!Yolov8_obb旋转框训练、测试、推理手把手教学(DOTA1.0数据集map50已达80%)

其中,images/train和images/val放置原始图片文件,labels/train_original和labels/val_original分别放置原始的标签文件,labels/train和labels/val为空,然后运行步骤(3)的代码,运行结束转换后的标签会保存在labels/train和labels/val中,格式如下。(4)新建yolov8-obb.yaml,修改nc和scales,我使用的是yolov8n.(2)构建数据集,安装下面目录格式,其他test可为空,一定要对应。

小嘤熊的博客 2万+

UVA-122-TREE ON THE LEVEL

UVA-122-TREE ON THE LEVELsubjectTrees are fundamental in many branches of computer science. Current state-of-the art parallel computers such as Thinking Machines’ CM-5 are based on fat trees. Quad- and

丹朗的博客 416

微信小程序PC端兼容性实战:如何绕过限制在电脑上运行手机端小程序

本文详细解析了微信小程序在PC端的兼容性问题及解决方案,重点介绍了如何利用`wx.getSystemInfo`API检测运行环境,并通过修改User-Agent、动态环境检测与适配等技术手段实现手机端小程序在PC端的顺畅运行。文章还提供了常见兼容性问题的解决方法和调试工具推荐,帮助开发者高效应对多端适配挑战。

weixin_28366053的博客 865

调用D3D11硬解码和渲染VideoProcessor版本

在https://blog.youkuaiyun.com/robothn/article/details/78781321一文中使用shader来显示FFMPEG硬解码后的YUV420P,本文调用D3D11的videoprocessor来进行图像空间变换和尺寸变换,取得了较好的效果。 1.初始化D3D11设备、videodevice等数据结构 FFMPEG调用D3D11硬解码,获取其对应的device,videodevice。。。 FFMPEG使用av_hwdevice_find_type_by_name调用D3d11

zlyadvocate的博客 9285

A - Trees on the level(树的层序遍历)

主要使用了c++STL中的set。用排序的方法对输入进行排序,根据字符的长度以及相同长度的字典序从小到大,正好对应树从上到下。1.某些节点无法从根节点出发到达。2.开始处理数据时都需要清空路径。...

zjsru_Beginner的博客 364

Qwen 三代进化全景对比:从 Qwen2.5 到 Qwen3 再到 Qwen3.5

本文对比了阿里巴巴通义千问三代大模型(Qwen2.5、Qwen3、Qwen3.5)的技术演进。18个月内,模型在架构、训练数据和能力上实现显著突破:从纯稠密结构(Qwen2.5)发展为混合注意力架构(Qwen3.5),支持语言从29种扩展至201种,并实现原生多模态融合。关键进展包括引入MoE架构、Gated DeltaNet混合注意力机制(计算效率提升10倍)、以及从独立模态到早期视觉语言联合训练的转变。模型阵容从7个通用尺寸发展为8+个混合架构,最大参数量达397B,上下文窗口扩展至256K(托管版1M

m0_47999117的博客 1万+

Uva - 122 - Trees on the level

难点在于二叉树的生成和宽度优先遍历BFS,最后要注意防止内存泄漏,虽然题目没有内存限制,但是优化考虑还是做一下释放内存,因为这个不是效率型的问题,测试时间短到没有无法显示。 AC代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <...

anpi3191的博客 178

IEEE投稿完整流程——以IEEE Transactions on Cybernetics为例【附:论文格式模板】

前言 以下是官方的流程,包括了论文的格式和提交的流程,重要!重要!重要! 在IEEE的官网中,也有论文提交的文件 可以直接参考: 中文版: 作者指南 英文版: Manuscripts-Author-Guide 作者指南的版本指导流程可能和最新版的官网不一致!!! 或者在下面的链接中,按照你的论文类型,选择不同的论文模板: IEEE article template 以下以IEEE Transactions on Cybernetics 为例子 IEEE Transactions on Cyberne

XC___XC的博客 10万+

抖音小程序:解决获取手机号报错 getPhoneNumber:fail auth deny,登录注册时需要获取用户手机号,getPhoneNumber一直提示错误(Uniapp抖音小程序报错也能解决)

抖音字节小程序获取手机号报错getPhoneNumber:fail auth deny,抖音 小程序 获取手机号 报错 getPhoneNumber:fail auth ,抖音小程序获取手机号始终返回拒绝,抖音小程序获取手机号,已经过了试运营期但依旧报错如何解决,调用 tt.getPhoneNumber 接口报错,登录注册时需要获取用户手机号,getPhoneNumber一直提示getPhoneNumber:fail auth deny,此能力仅支持小程序通过试运营期后可用,默认获取权限,无需申请;unia

✅ 精选推荐文章 4304

uva122 trees on the level

看不下去,找点简单的,再回来写

Dosth_Magic 625

ADS基础教程23 - 有限元电磁仿真(FEM)可视化操作

一、引言 二、FEM可视化操作流程1.打开可视化界面2.查看介质的网格3.设置网格颜色4.选择网格5.传感器选择6.编辑传感器7.选择频率8.动画 三、总结介绍了在ADS中如何进行FEM仿真场视图操作过程。

XZ_ZC的博客 1815

(二叉树建树 先序遍历)UVA_122 Trees on the level

UVA_122 Trees on the level题意让我们构建一棵树,给出每一个节点是从根节点怎么走才能走到. 比如(11,LL)就是根节点的左子树的左子节点的位置权值为11.然后让给出这棵树的先序遍历结果. 如果出现下列情况,输出”not complete” 重复赋值 没有根节点 有节点没有被赋值 给出超过一个节点 解决这个题的输入比较坑啊,刚上来感觉解决不了…

ZhangYang 410

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

UVA-122(Trees on the level)

Trees on the level 题目链接: https://vjudge.net/problem/UVA-122 题目意思: 给你一些(,)让你建立一棵树,直到输入()结束建树,然后判断树是否完整,如果没有结点未赋值或者被赋值两次,就按层次遍历输出树,否则输出not complete 代码: #include <algorithm> #include <m...

weixin_30278237的博客 90

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

UVA.122 Trees on the level(二叉树 BFS)

UVA.122 Trees on the level(二叉树 BFS)题意分析给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete代码总览#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <vector> #

Pengwill's Blog 361

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Trees on the level UVA - 122

Trees on the level UVA - 122 紫书6.3.2 二叉树的层次遍历 采用动态结构创建二叉树 #include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <queue> using namespace s...

NaNa 154

[UVA] Trees on the level

题目:https://vjudge.net/problem/UVA-122 #include<cstdio> #include<cstring> #include<vector> #include<queue> #pragma warning(disable:4996) using namespace std; const int maxn =...

weixin_37992828的博客 163

122 Trees on the level

一个二叉树的题; 看的书上和网上的结合,用一个动态结构构造一棵树,比较麻烦,但好理解 #include #include #include using namespace std; const int maxn=260; int ans[maxn];//用来装输出的数的结点值 struct Node { bool have_value;//是否被定义 int v;//结

DS00HY的小博客 274

122 - Trees on the level

Background Trees arefundamental in many branches of computer science. Current state-of-the artparallel computers such as Thinking Machines' CM-5 are based on fattrees. Quad- and octal-trees are funda

gaoxiangnumber1——Gao Xiang's Blog 483
上一篇: 二叉搜索树
下一篇: The order of a Tree
admin__
博客等级 码龄8年 3粉丝 79原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值