题目给出一个后缀表达式,让你求从下往上的层次遍历。

思路:结构体建树,然后用数组进行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层次遍历的更多相关文章

  1. UVa 11234 Expressions (二叉树重建&由叶往根的层次遍历)

    画图出来后结果很明显 xyPzwIM abcABdefgCDEF sample output wzyxIPM gfCecbDdAaEBF * + - x y z w F B E a A d D b c ...

  2. leetcode@ [126] Word Ladder II (BFS + 层次遍历 + DFS)

    https://leetcode.com/problems/word-ladder-ii/ Given two words (beginWord and endWord), and a diction ...

  3. hdu 1622 Trees on the level(二叉树的层次遍历)

    题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS ( ...

  4. 树的层次遍历(Trees on the level,UVA 122)

    题目描述: 题目思路: 1.用结构链表来建树 2.用队列来实现层次遍历,当遍历到根节点时,将其子节点压入队列 #include <iostream> #include <cstdli ...

  5. Trees on the level UVA - 122 (二叉树的层次遍历)

    题目链接:https://vjudge.net/problem/UVA-122 题目大意:输入一颗二叉树,你的任务是按从上到下,从左到右的顺序输出各个结点的值.每个结点都按照从根节点到它的移动序列给出 ...

  6. UVA - 122 Trees on the level (二叉树的层次遍历)

    题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点. 分析:先建树,建树的过程中,沿途结点都申请了 ...

  7. UVa 122 Trees on the level(链式二叉树的建立和层次遍历)

    题目链接: https://cn.vjudge.net/problem/UVA-122 /* 问题 给出每个节点的权值和路线,输出该二叉树的层次遍历序列. 解题思路 根据输入构建链式二叉树,再用广度优 ...

  8. UVa 122 树的层次遍历

    题意: 给定一颗树, 按层次遍历输出. 分析: 用数组模拟二叉树, bfs即可实现层次遍历 #include <bits/stdc++.h> using namespace std; st ...

  9. 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 ...

随机推荐

  1. 实时监控MySql状态

    大多网站的性能瓶颈都会出在数据库上,所以想把Mysql监控起来,就搜索了下相关资料. 后来和同事讨论了下cacti和nagios有些老套和过时,graphite比较时尚,然后就搜了下相关的资料,最后搞 ...

  2. 未能加载文件或程序集“Oracle.DataAccess, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342”或它的某一个依赖项。 解决方法

    webconfig文件对于oracle的映射错误.需要在以下位置修改 <runtime> <legacyCorruptedStateExceptionsPolicy enabled= ...

  3. Apache服务器配置默认首页文件名和网站路径

    默认首页的配置: 第一种:直接修改apache服务器的配置文件./conf/httpd.conf中的DirectoryIndex,如:(项目web以index.php为首页) <IfModule ...

  4. ios和android一并学习的体会

    如果说为什么要同时学习这两种不同的移动平台,其实有一定的“闲”的因素在里面. 相对于ios,android我是早半年接触的.最开始学习的时候也就是j2ee学习的延续,通过看视频连带看书学了大概一个月的 ...

  5. jquery获取kindEditor值

    KE.show({            id: 'txtMessage',            imageUploadJson: '/ajax/Manager/keupload.ashx?ptyp ...

  6. 图片懒加载 lazyload

    添加引用 <script type="text/javascript" src="lazyload/yahoo-dom-event.js">< ...

  7. Binding的Source从何而来?

    I. Binding to Object 1. Binding data using ObjectDataProvider AC:Let’s say there is a CLR based data ...

  8. MySQL事务处理和锁机制

    事务处理和并发性 1.1 基础知识和相关概念 1 )全部的表类型都可以使用锁,但是只有 InnoDB 和 BDB 才有内置的事务功能. 2 )使用 begin 开始事务,使用 commit 结束事务, ...

  9. 常见MFC UI界面库[转]

    Xtrme toolkit,BCGControlBar,SkinMagic,AppFace,Skin++,Uskin++,SYGUI,LibUIDK,GuiToolkit,GardenUI等等,除了后 ...

  10. 学习C++的一些问题总结

    C++ 问题 (一) int main() { int i,j,m,n; i=8; j=10; m=++i+j++;  //++i是先递加再使用,j++是先使用再递加,故:9+10=19 n=++i+ ...