uva 11234 Expressions 表达式 建树+BFS层次遍历
题目给出一个后缀表达式,让你求从下往上的层次遍历。
思路:结构体建树,然后用数组进行BFS进行层次遍历,最后把数组倒着输出就行了。
uva过了,poj老是超时,郁闷。
代码:
#include <cstdio>
#include <cstring>
#include <cstdlib> const int maxn = 10001;
char str[maxn];
int p; struct Node {
char data;
Node* l;
Node* r;
}; Node* build() {
Node* u = (Node*) malloc (sizeof(Node*));
u -> data = str[p];
p--;
if (str[p] >= 'A' && str[p] <= 'Z')
u -> l = build();
else
u -> l -> data = str[p];
p--;
if (str[p] >= 'A' && str[p] <= 'Z')
u -> r = build();
else
u -> r -> data = str[p];
return u;
} int main() {
int t;
while (scanf("%d", &t) != EOF) {
scanf("%s", str);
p = strlen(str) - 1;
Node* root = build();
int rear = 0, front = 0;
Node* q[maxn];
q[rear++] = root;
while (rear > front) {
if (q[front]) {
if (q[front] -> r)
q[rear++] = q[front] -> r;
if (q[front] -> l)
q[rear++] = q[front] -> l;
front++;
}
else
front++;
}
for (int i = rear - 1; i >= 0; i--)
printf("%c", q[i]->data);
printf("\n");
}//while
}
uva 11234 Expressions 表达式 建树+BFS层次遍历的更多相关文章
- UVa 11234 Expressions (二叉树重建&由叶往根的层次遍历)
画图出来后结果很明显 xyPzwIM abcABdefgCDEF sample output wzyxIPM gfCecbDdAaEBF * + - x y z w F B E a A d D b c ...
- leetcode@ [126] Word Ladder II (BFS + 层次遍历 + DFS)
https://leetcode.com/problems/word-ladder-ii/ Given two words (beginWord and endWord), and a diction ...
- hdu 1622 Trees on the level(二叉树的层次遍历)
题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS ( ...
- 树的层次遍历(Trees on the level,UVA 122)
题目描述: 题目思路: 1.用结构链表来建树 2.用队列来实现层次遍历,当遍历到根节点时,将其子节点压入队列 #include <iostream> #include <cstdli ...
- Trees on the level UVA - 122 (二叉树的层次遍历)
题目链接:https://vjudge.net/problem/UVA-122 题目大意:输入一颗二叉树,你的任务是按从上到下,从左到右的顺序输出各个结点的值.每个结点都按照从根节点到它的移动序列给出 ...
- UVA - 122 Trees on the level (二叉树的层次遍历)
题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点. 分析:先建树,建树的过程中,沿途结点都申请了 ...
- UVa 122 Trees on the level(链式二叉树的建立和层次遍历)
题目链接: https://cn.vjudge.net/problem/UVA-122 /* 问题 给出每个节点的权值和路线,输出该二叉树的层次遍历序列. 解题思路 根据输入构建链式二叉树,再用广度优 ...
- UVa 122 树的层次遍历
题意: 给定一颗树, 按层次遍历输出. 分析: 用数组模拟二叉树, bfs即可实现层次遍历 #include <bits/stdc++.h> using namespace std; st ...
- PAT-2019年冬季考试-甲级 7-4 Cartesian Tree (30分)(最小堆的中序遍历求层序遍历,递归建树bfs层序)
7-4 Cartesian Tree (30分) A Cartesian tree is a binary tree constructed from a sequence of distinct ...
随机推荐
- python 加密解密
1. 使用base64 s1 = base64.encodestring('hello world') s2 = base64.decodestring(s1) print s1, s2 结果 1 2 ...
- UI进阶 科大讯飞(1) 语音听写(语音转换成文字)
一.科大讯飞开放平台: http://www.xfyun.cn/ 注册.登录之后创建新应用. 因为本项目只实现了语音听写,所以在SDK下载中心勾选语音听写单项SDK就可以了 开发平台选择iOS,应用选 ...
- JedisPool连接池实现难点
[http://jiangwenfeng762.iteye.com/blog/1280700] [可改进的问题] 问题是jedispool有没有办法监控状态,比如说当前连接有多少,当前idle连接 ...
- Ext.form.ComboBox 后台取值 动态加载 ext5.0.0
我用的extjs是5.0.0版本的. 请注意:如果这里没有的combobox相关内容,这里一定有. 开始的时候keyup事件取到的数据就是放不到ComboBox中,放全局变量也不好用.最后大神出手帮忙 ...
- How to Send Information (String, Image, Record) Between Two Applications
http://delphi.about.com/od/windowsshellapi/a/wm_copydata.htm here are many situation when you need t ...
- AC、HC、AHC、ACT、LS的区别
http://forum.eet-cn.com/thread!printPreview.jspa?threadID=1200029698&start=0 以245为例,74AC245.74HC ...
- Java面试试题
第一,谈谈final, finally, finalize的区别.最常被问到. 第二,Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以impl ...
- jQuery.FlexiGrid使用总结
经过对FlexiGrid的大量使用,及时不时琢磨下其代码,对她的脾性有了一定的了解,是该做总结的时候了. 一.FlexiGrid下载 1.原版代码 最近Paulo P. Marinas对FlexiGr ...
- IOS 7 Study - Displaying an Image on a Navigation Bar
ProblemYou want to display an image instead of text as the title of the current view controlleron th ...
- BZOJ 1014: [JSOI2008]火星人prefix Splay+二分
1014: [JSOI2008]火星人prefix 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1014 Description 火星人 ...