UVA表达式树的后序遍历和层次遍历和建树
来源:互联网 发布:淘宝店怎么样提高销量 编辑:程序博客网 时间:2024/06/05 18:01
太笨又太蠢,做的小白书后面的题,前面写的线性表部分的内容,就一直在想线性表的做法,
想vector,想stack,结果是表达式树,建立二叉树根据后缀表达式,然后层次遍历一遍反转就行了。
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<string>#include<cctype>#include<cmath>#include<map>#include<set>#include<vector>#include<queue>#include<stack>#include<ctime>#include<algorithm>#include<sstream>#define LL long longusing namespace std;const int maxn=1e4+5;int main(){ int t; cin>>t; getchar(); while(t--) { string s; getline(cin,s); string re=""; stack<int> v; int left[maxn],right[maxn]; for(int i=0;i<s.length();i++) { if(islower(s[i])) { v.push(i); left[i]=right[i]=-1; } else { int rr=v.top(); v.pop(); int ll=v.top(); v.pop(); v.push(i); left[i]=ll; right[i]=rr; } } queue<int> q; q.push(v.top()); while(!q.empty()) { int ai=q.front(); q.pop(); re=s[ai]+re; if(left[ai]!=-1) q.push(left[ai]); if(right[ai]!=-1) q.push(right[ai]); } cout<<re<<endl; } return 0;}
0 0
- UVA表达式树的后序遍历和层次遍历和建树
- 二叉树的建树、遍历(先序、中序、后序、层次)(递归和非递归)--Java实现
- 二叉树的后序遍历和层次遍历
- UVa 548 Tree 根据后序遍历和中序遍历建树后DFS
- 二叉树-建树,层次遍历,先序遍历,中序遍历,后序遍历
- uva 11234 Expressions 表达式 建树+BFS层次遍历
- 由后序遍历和中序遍历建树
- 数据结构——二叉树的建立和遍历(递归建树&层序遍历建树)
- 二叉树 根据前序遍历和中序遍历 或者 后序遍历和中序遍历建树
- 由二叉树的后序和中序求层次遍历
- 例题 6-7 树的层次遍历 UVa 122 建树+宽度优先遍历
- 建树和遍历树(前序)
- UVA 11234 - Expressions 二叉树建树+BFS 层次遍历
- BST二叉搜索树的建树和先序遍历
- 二叉搜索树的建树和中序遍历操作
- 模板-根据中序,后序建树层次遍历输出
- 通过前序和后序遍历数组重建树
- 例题6-8 树 Tree UVa 548 中序遍历+后序遍历建树
- Perl学习(五)
- OC不可变数组NSArray详解
- 通过configSource提高web.config配置灵活性
- 用C语言求一元二次方程的根
- 内存堆和栈的区别
- UVA表达式树的后序遍历和层次遍历和建树
- 【Cocos2dx】使用CCScale9Sprite拉伸图片
- 指针
- 黑马程序员——集合
- gcc与g++
- 矩阵结构体
- Java 线程测试框架
- C++那些细节--函数的默认参数
- hdu 5414 CRB and String 2015多校联合训练赛#10 贪心