SDUT 3344 数据结构实验之二叉树五:层序遍历
来源:互联网 发布:miflash 保留所有数据 编辑:程序博客网 时间:2024/03/29 17:19
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=3344
数据结构实验之二叉树五:层序遍历
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
已知一个按先序输入的字符序列,如abd,,eg,,,cf,,,(其中,表示空结点)。请建立二叉树并求二叉树的层次遍历序列。
输入
输入数据有多行,第一行是一个整数t (t<1000),代表有t行测试数据。每行是一个长度小于50个字符的字符串。
输出
输出二叉树的层次遍历序列。
示例输入
2abd,,eg,,,cf,,,xnl,,i,,u,,
示例输出
abcdefgxnuli
提示
来源
xam
示例程序
- 提交
- 状态
- 讨论
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- char s[50+5];
- int ans;
- struct tree
- {
- char data;
- tree *l, *r;
- }*q[50+5];
- tree *build() //递归先序建立二叉树
- {
- char c=s[ans++];
- tree *root;
- if(c==',')
- root=NULL;
- else
- {
- root=new tree;
- root->data=c;
- root->l=build();
- root->r=build();
- }
- return root;
- }
- void level(tree *root) //基于队列的层次遍历
- {
- if(!root) return ;
- int head=1, tail=1;
- q[tail++]=root;
- while(head<tail)
- {
- printf("%c", q[head]->data);
- if(q[head]->l)
- {
- q[tail++]=q[head]->l;
- }
- if(q[head]->r)
- {
- q[tail++]=q[head]->r;
- }
- head++;
- }
- }
- int main()
- {
- int t;
- while(~scanf("%d", &t))
- {
- while(t--)
- {
- ans=0;
- scanf("%s", s);
- tree *root=build();
- level(root);
- printf("\n");
- }
- }
- return 0;
- }
0 0
- SDUT 3344 数据结构实验之二叉树五:层序遍历
- SDUT 3344 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历 (sdut OJ 3344)
- SDUT 3344 数据结构实验之二叉树五:层序遍历
- SDUT-3344 数据结构实验之二叉树五:层序遍历
- [SDUT](3344)数据结构实验之二叉树五:层序遍历
- SDUT OJ 数据结构实验之二叉树五:层序遍历
- sdut oj3344 数据结构实验之二叉树五:层序遍历
- 3344---数据结构实验之二叉树五:层序遍历
- 3344 数据结构实验之二叉树五:层序遍历
- 3344-->数据结构实验之二叉树五:层序遍历
- 3344 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- 数据结构实验之二叉树五:层序遍历
- Codeforces 240E Road Repairs(最小树形图路径)
- Android事件分发机制完全解析,带你从源码的角度彻底理解(下)
- Apache Zeppelin使用入门指南:编程
- 蛇形布线&0Ω电阻
- VMware虚拟机如何使用
- SDUT 3344 数据结构实验之二叉树五:层序遍历
- 【贪心+优先队列】1428 活动安排问题【51nod】【难度:2级算法题】
- hdoj2029
- Hibernate_ManyToMany_Demo
- 编译模块时遇到Invalid module format
- 动态规划-3003-序列的最大上升子序列
- SourceInsight代码工程
- 统计学 入门基础概念篇 - Descriptive Statistics: Quantitative Measures(个人笔记)
- 环境变量的访问及设置