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

思路:结构体建树,然后用数组进行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. 关于select @@IDENTITY的初识

    这句话主要是得到唯一的主键,然后应用于下面的SQL语句 例如代码 StringBuilder strSql=new StringBuilder(); strSql.Append("inser ...

  2. POJ 3175 Finding Bovine Roots (暴力求解)

    题意:给定一串数字,问你这是一个数字开方根得到的前几位,问你是哪个数字.析:如果 x.123... 这个数字的平方是一个整数的话,那必然sqr(x.124) > ceil(sqr(x.123)) ...

  3. System.Data.SqlTypes.SqlNullValueException: 数据为空。不能对空值调用此方法或

    有可能读出的数据为NULL,可以这样改: 方法一:while (reader.Read()){ for (int i = 0; i < 7; i++) { if (reader.IsDBNull ...

  4. halcon的性能

    ·满足您各类机器视觉应用需求的完善的开发库 ·包含匹配,识别,定位及1D,2D,3D测量等多种高级算法 ·强大,易用的工具加速您的开发进程 ·与Linux/UNI及Windows(包括×64)兼容,避 ...

  5. 备份Xcode6的配色主题以及代码模板

    ~/Library/Developer/Xcode/UserData/FontAndColorThemes ~/Library/Developer/Xcode/UserData/CodeSnippet ...

  6. Toast在关闭应用后还显示的解决办法

    1.我们在用Toast的用法就是:Toast.makeText(Context,CharSequence , Duration).show().但有的时候如果你在一次操作当中多次点击一个view的时候 ...

  7. Java工具类 Apache Commons:commons-lang

    Commons Lang The standard Java libraries fail to provide enough methods for manipulation of its core ...

  8. ASSER、VERIFY、TRACE详解

    ASSERT()被测试它的参数,如果参数为零,则中断执行并打印一段说明消息.在Release版本的程序中它不起任何作用. ASSERT()使用的时候必须保证参数表达式中不能有函数调用,因此对于任何有函 ...

  9. css margin的相关属性,问题及应用

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=48 引言:margin ...

  10. 使用RemObjects Pascal Script (转)

    http://www.cnblogs.com/MaxWoods/p/3304954.html 摘自RemObjects Wiki 本文提供RemObjects Pascal Script的整体概要并演 ...