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 ...
随机推荐
- JS制作的简单的三级及联
前台: <form id="form1" runat="server"> <div> 省 <select id="Pro ...
- HDU4289Control(最大流)
看了这道题,然后重新开始练习自己的刚敲不久的网络流,发现还是难以一遍敲得完整啊,,,,, 调了...遍,改了...遍,测了...遍,交了,,,遍,总算是A了,,不简单啊 然后试着用了其他两种算法EK和 ...
- POJ3080Blue Jeans(暴力)
开始做字符串专题,地址 第一题水题,暴力就可以做 #include <map> #include <set> #include <stack> #include & ...
- MFC中消息响应机制
由于视类窗口始终覆盖在框架类窗口之上,因此所有操作,包括鼠标单击.鼠标移动等操作都只能由视类窗口捕获.一个MFC消息响应函数在程序中有三处相关信息:函数原型.函数实现和以及用来关联消息和消息响应函数的 ...
- winform 发布应用程序 提示 “未能注册模块(程序路径)\ieframe.dll”
程序安装的时候出现未能注册模块(程序路径)\ieframe.dll提示 这种情况的出现,是因为引用的shdocvw.dll,目前发现了一个折中的解决方法,在安装程序里面,可以看到ieframe.dll ...
- 一个优秀windows C++程序员的知识体系[转]
转自:一个优秀windows C++程序员的知识体系 思考一个优秀windows C++ 程序员该有哪些知识,可最终发现什么知识都不能少, 看下图: 除了上面知识,程序员还要不断学习, 保持对新知识的 ...
- JS Flex交互:html嵌套Flex(swf)
一.html页面嵌套Flex需要用到 swfobject.js swfobject的使用是非常简单的,只需要包含 swfobject.js这个js文件,然后在DOM中插入一些简单的JS代码,就能嵌入F ...
- Cocos2d-x利用CCHttpRequest获取网络图片并显示
利用CCHttpRequest获取网上http地址的图片并缓存到本地生成CCSprite用于显示 //图片结构class imgstruct : public CCObject { public: i ...
- Android+Jquery Mobile学习系列(4)-页面跳转及参数传递
关于页面转场,这个必须得专门列出来说明一下,因为Jquery Mobile与普通的Web发开有一些区别,这个对于新手如果不了解的话,就会钻到死胡同.撸主前段时间就是很急躁地上手开发程序,结果在页面转场 ...
- php计算脚本执行时间
利用PHP的microtime实现 function getCurrentTime () { list ($msec, $sec) = explode(" ", microtime ...