UVA 11234 - Expressions 二叉树建树+BFS 层次遍历
来源:互联网 发布:西安58同城网络推广 编辑:程序博客网 时间:2024/05/16 17:35
RT
给出了一个后缀表达式 要给出用队列表示时的答案 看simple output 知道输出时按照数的层数 从下到上从右到左一一输出的
#include<cstdlib>#include<algorithm>#include<cctype>#include<stack>#include<cstdio>#include<cstring>#include<queue>using namespace std;int maxn = 10005;typedef struct TNode{ char v; struct TNode *l,*r;//左节点右节点}Node;stack<Node*> st;queue<Node*> q;Node* newnode(){ Node* u = (Node*)malloc(sizeof(Node)); if(u != NULL) u->l = u->r = NULL; return u;}void addnode(char* s){ while(!st.empty())st.pop(); Node* u; for(int i = 0; i < strlen(s); i++) { if(islower(s[i])) { u = newnode(); u->v = s[i]; st.push(u); } else { u = newnode(); u->r = st.top(); st.pop(); u->l = st.top(); st.pop(); u->v = s[i]; st.push(u); } }}int n = 0;void BFS()//层次遍历{ while(!q.empty())q.pop(); char p[maxn]; memset(p,0,sizeof(p)); q.push(st.top()); while(!q.empty()) { Node* u = q.front(); q.pop(); p[n++] = u->v; if(u->l != NULL) q.push(u->l); if(u->r != NULL) q.push(u->r); } for(int i = n-1; i >= 0;i --) printf("%c",p[i]); printf("\n");}int main(){ #ifdef LOCAL freopen("in.txt","r",stdin); #endif // LOCAL int t; scanf("%d",&t); while(t--) { char s[maxn]; scanf("%s",s); n = 0; addnode(s); BFS(); } return 0;}
0 0
- UVA 11234 - Expressions 二叉树建树+BFS 层次遍历
- uva 11234 Expressions 表达式 建树+BFS层次遍历
- UVa 11234 Expressions 二叉树 层次遍历 广搜
- Uva 11234 Expressions (二叉树的层次遍历)
- uva 11234 Expressions(建立二叉树+层次遍历)
- UVa 11234 Expressions (二叉树重建&由叶往根的层次遍历)
- [UVA 11234] Expressions (二叉树的建立与层次遍历)
- 二叉树层次建树.
- 数据结构,二叉树已知后续中序,建树,层次遍历;
- 二叉树建立 以及 bfs层次遍历
- 初始BFS ,以及二叉树层次遍历
- 二叉树-建树,层次遍历,先序遍历,中序遍历,后序遍历
- uva 122 Trees on the level 二叉树的层次遍历(bfs)
- UVA 699(二叉树建树与遍历)
- UVA表达式树的后序遍历和层次遍历和建树
- 例题 6-7 树的层次遍历 UVa 122 建树+宽度优先遍历
- 数据结构 + BFS uva 11234 - Expressions
- Uva 11234 - Expressions//数据结构,二叉树
- 常用的进程计算脚本
- George and Sleep
- LeetCode OJ:Integer to Roman
- HDOJ 1.1.8 A+B for Input-Output Practice (VIII)
- LeetCode OJ:Container With Most Water
- UVA 11234 - Expressions 二叉树建树+BFS 层次遍历
- George and Round
- LeetCode OJ:Regular Expression Matching
- Quarter
- ffmpeg 指令
- cubieboard nand 重新分区扩容
- EntityFramework之领域驱动设计实践【分层架构】
- ffmpeg 参数
- EntityFramework之领域驱动设计实践【销售系统】