Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with parentheses reflecting the precedences of the operators.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 20) which is the total number of nodes in the syntax tree. Then N lines follow, each gives the information of a node (the i-th line corresponds to the i-th node) in the format:

data left_child right_child

where data is a string of no more than 10 characters, left_child and right_child are the indices of this node's left and right children, respectively. The nodes are indexed from 1 to N. The NULL link is represented by −1. The figures 1 and 2 correspond to the samples 1 and 2, respectively.



Figure 1



Figure 2

Output Specification:

For each case, print in a line the infix expression, with parentheses reflecting the precedences of the operators. Note that there must be no extra parentheses for the final expression, as is shown by the samples. There must be no space between any symbols.

Sample Input 1:

8
* 8 7
a -1 -1
* 4 1
+ 2 5
b -1 -1
d -1 -1
- -1 6
c -1 -1

Sample Output 1:

(a+b)*(c*(-d))

Sample Input 2:

8
2.35 -1 -1
* 6 1
- -1 4
% 7 8
+ 2 3
a -1 -1
str -1 -1
871 -1 -1

Sample Output 2:

(a*2.35)+(-(str%871))
#include<iostream> //海星,建树然后中序遍历
#include<vector>
using namespace std;
struct node{
string val;
node* left;
node* right;
node(string v):val(v), left(NULL), right(NULL){
}
};
vector<int> lc, rc;
vector<string> data;
node* buildtree(node* root, int r){
root=new node(data[r]);
if(lc[r]!=-1)
root->left=buildtree(root->left, lc[r]);
if(rc[r]!=-1)
root->right=buildtree(root->right, rc[r]);
return root;
}
void solution(node* root, int flag){
if((root->left||root->right)&&flag)
cout<<"(";
if(root->left)
solution(root->left, 1);
cout<<root->val;
if(root->right)
solution(root->right, 1);
if((root->left||root->right)&&flag)
cout<<")";
}
int main(){
int n;
cin>>n;
vector<int> visited(n+1, 0);
lc.resize(n+1), rc.resize(n+1);
data.resize(n+1);
for(int i=1; i<=n; i++){
int l, r;
cin>>data[i]>>l>>r;
lc[i]=l;
rc[i]=r;
if(l!=-1) visited[l]=1;
if(r!=-1) visited[r]=1;
}
int i;
for(i=1; i<=n; i++)
if(visited[i]==0)
break;
node* root=NULL;
root=buildtree(root ,i);
solution(root, 0);
return 0;
}

PAT 1130 Infix Expression的更多相关文章

  1. PAT 1130 Infix Expression[难][dfs]

    1130 Infix Expression (25 分) Given a syntax tree (binary), you are supposed to output the correspond ...

  2. PAT甲级 1130. Infix Expression (25)

    1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...

  3. PAT甲级——1130 Infix Expression (25 分)

    1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...

  4. PAT 甲级 1130 Infix Expression

    https://pintia.cn/problem-sets/994805342720868352/problems/994805347921805312 Given a syntax tree (b ...

  5. PAT A1130 Infix Expression (25 分)——中序遍历

    Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...

  6. 1130. Infix Expression (25)

    Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...

  7. PAT甲题题解-1130. Infix Expression (25)-中序遍历

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789828.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  8. 1130 Infix Expression

    题意:给出一个语法树(二叉树),输出相应的中缀表达式. 思路:很显然,通过中序遍历来做.通过观察,发现除了根结点之外的所有非叶结点的两侧都要输出括号,故在中序遍历时判断一下即可. 代码: #inclu ...

  9. PAT1130:Infix Expression

    1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...

随机推荐

  1. 通过CSS控制页面中的内容垂直居中的方法

    方法一:通过行高(line-height)定位 line-height通常是用于调节一段文字的行与行之间的距离,或者说两行文字之间的距离,如果行高是500px,那么每一行中的文字距离本行的顶部就是25 ...

  2. ASP.Net 下载大文件的实现 (转)

    原文:http://www.cnblogs.com/luisliu/p/4253815.html 当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃 ...

  3. 解决Error for wireless request "Set Mode" (8B06) 问题 (转载)

    转自:http://blog.csdn.net/muge0913/article/details/17062871 在运行以下命令的时候,意外的出错,最后google了下,最终才确定了原因,因为在运行 ...

  4. 【Java】3到5年开发常见的Java面试题

    一.Java基础和高级 String类为什么是final的. HashMap的源码,实现原理,底层结构. 反射中,Class.forName和classloader的区别 session和cookie ...

  5. 【原创】Eclipse实现图形化界面插件-vs4e

    vs4e插件下载地址:http://visualswing4eclipse.googlecode.com/files/vs4e_0.9.12.I20090527-2200.zip 下载完成后,解压,然 ...

  6. 制作并发布个人CocoaPods库

    随着对 CocoaPods 越来越多的依赖,我们也可以尝试把自己的库发布到它上面. 1.在Github上新建一个项目(名字我随便取了一个,其他步骤截图为WCUIKit).自己做相应修改即可. 2.克隆 ...

  7. Maven之项目搭建与第一个helloworld(多图)

    这次记录第一个搭建一个maven的helloworld的过程. 转载 1.搭建web工程肯定得new 一个 maven工程,假如project中没有直接看到maven工程,那么选择Other,然后在W ...

  8. TCP/IP与Http与socket的关系

    1 理清概念: TCP/IP是一个大的协议族(只不过TCP和IP是super star所以就这么命名了),它包括了: 应用层协议:FTP.HTTP.TELNET.SMTP.DNS(协议): 传输层协议 ...

  9. The Chosen One

    https://www.hackerrank.com/contests/101hack45/challenges/the-chosen-one 找出一个数字,使得,数组中只有一个数字不是这个数的约数, ...

  10. headroom.js使用

    为页面顶部多留些空间.在不需要页头时将其隐藏 需要添加的css代码 .headroom { transition: transform 200ms linear; } .headroom--pinne ...