hdu 1805 Expressions (建树与层序遍历)
来源:互联网 发布:pd1治疗哪些癌症知乎 编辑:程序博客网 时间:2024/06/06 07:26
#include <iostream>#include <stdlib.h>#include <stack>#include <string.h>#include <queue>#define maxn 100101using namespace std;int flag;typedef struct BiNode{ char value; struct BiNode *left,*right;} BiNode,*BiTree;int main(){ int n,cnt; char str[maxn]; char ans[maxn]; stack<BiTree>s; cin>>n; while(n--) { cin>>str; int len = strlen(str); for(int i = 0; i < len; i++) { if(str[i]>='a'&&str[i]<='z') { BiTree u=(BiTree)malloc(sizeof(BiNode)); u->value=str[i]; u->left=u->right=NULL; s.push(u); } else if(str[i]>='A'&&str[i]<='Z') { BiTree x,y; x=s.top(); s.pop(); y=s.top(); s.pop(); BiTree u=(BiTree)malloc(sizeof(BiNode)); u->value=str[i]; u->left=y; u->right=x; s.push(u); } } BiTree u=s.top(); queue<BiTree>q; cnt=0; q.push(u); while(!q.empty()) { BiTree v=q.front(); q.pop(); ans[cnt++]=v->value; if(v->left)q.push(v->left); if(v->right)q.push(v->right); } for(int i=cnt-1; i>=0; i--) cout<<ans[i]; cout<<endl; while(!s.empty())s.pop(); } return 0;}
阅读全文
0 0
- hdu 1805 Expressions (建树与层序遍历)
- 根据后序与中序遍历建树层序遍历输出
- 先序建树,中序遍历、层序遍历
- 数据结构——二叉树的建立和遍历(递归建树&层序遍历建树)
- EOJ1811 根据层号表示建树并后序遍历
- 树:前序建树,前序遍历,中序遍历,后序遍历,层序遍历
- uva 11234 Expressions 表达式 建树+BFS层次遍历
- UVA 11234 - Expressions 二叉树建树+BFS 层次遍历
- 已知树的先中序遍历--不建树--得到层序遍历
- HDU 5444-二叉树的遍历+建树
- hdu-5444(简单建树和遍历)
- hdu 5444Elven Postman(建树,遍历)
- 顺序建树后序遍历
- 天梯赛L2-011玩转二叉树----已知前序中序建树---输出层序遍历
- HDU 5444 二叉树的遍历,先根据先序遍历建树
- 用java实现二叉树相关操作(前序建树,前中后递归非递归遍历,层序遍历)
- 根据前序与中序遍历建树后以后序遍历输出
- 建树和遍历树(前序)
- 分治算法
- spring常用注解
- 分析M16C62在uC/OS-II嵌入式系统中的应用
- 奥利奥 Android 8.0 新功能和 API
- PLSQL DEVELOPER 12.05注册机
- hdu 1805 Expressions (建树与层序遍历)
- css设置中文字体指南
- python day09
- 使用margin还是padding
- 深入理解margin
- S19文件格式简述
- Windows下,运行tomcat9w.exe出现unable to open the service tomcat9问题解决办法。
- 负margin原理&应用
- vim编程环境准备