1130. Infix Expression (25)
来源:互联网 发布:云头条 | 云计算云媒体 编辑:程序博客网 时间:2024/05/16 14:41
#include<cstdio>#include<iostream>#include<string>#include<algorithm>using namespace std;const int maxn=30;struct Node{ string data; int left,right;};Node tree[maxn];bool isRoot[maxn];int tRoot;string ans;void inOrder(int root){ if(tree[root].left!=-1){ if(root!=tRoot) ans.append("("); inOrder(tree[root].left); } if(tree[root].data=="-"&&tree[root].left==-1&&root!=tRoot){ //如果没有root!=tRoot,测试点三错误,对应以下case //2 //- -1 2 //3 -1 -1 ans.append("("); } ans.append(tree[root].data); if(tree[root].right!=-1){ inOrder(tree[root].right); if(root!=tRoot) ans.append(")"); }}int main(){ int n; scanf("%d",&n); fill(isRoot,isRoot+maxn,true); for(int i=1;i<=n;i++){ string data; int l,r; cin>>data>>l>>r; tree[i].data=data; tree[i].left=l; tree[i].right=r; isRoot[l]=isRoot[r]=false; } for(int i=1;i<=n;i++){ if(isRoot[i]==true){ tRoot=i; break; } }// printf("root:%d\n",tRoot); inOrder(tRoot); cout<<ans<<endl; return 0; }
阅读全文
0 0
- 1130. Infix Expression (25)
- 1130. Infix Expression (25)
- 1130. Infix Expression (25)
- 1130. Infix Expression (25)
- 1130. Infix Expression (25)
- 1130. Infix Expression (25)
- 1130. Infix Expression (25)
- PAT 1130. Infix Expression (25)
- PAT 1130. Infix Expression (25)
- PAT 1130. Infix Expression (25)-甲级
- PAT甲级 1130. Infix Expression (25)
- 【PAT】【Advanced Level】1130. Infix Expression (25)
- PAT 甲级 1130. Infix Expression (25)
- 1130. Infix Expression 引用
- PAT A1130. Infix Expression (25)
- 1130. Infix Expression (25)[dfs-中序遍历]
- PAT--1130. Infix Expression(中序加括号)
- PAT(A)-1130. Infix Expression (25)(树的中序遍历)
- jQuery ajax读取本地json文件
- AS 中 Plugin for Gradle 和 Gradle 之间的版本对应关系
- GCC15个命令汇总
- 关于keep-alive的几点疑惑
- 第五周项目3括号的匹配
- 1130. Infix Expression (25)
- PageRank算法及MapReduce实现
- 712. Minimum ASCII Delete Sum for Two Strings
- 三角形的边
- ROS学习笔记(六)
- Android在application中使用SharedPreference注意事项
- unity transform.parent和SetParent效率差了几万倍?
- 解决Ubuntu,linux mint 无法登录(输入正确用户名和密码仍跳回登录界面)
- 171129-用随机函数初始化数组并逆向输出【连续第三十五天】