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<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
typedef struct{
int lchild, rchild;
string data;
}node;
node tree[];
int N, notRoot[] = {};
void inOrder(int root, int first){
if(root == -)
return;
string ss = tree[root].data;
if(tree[root].rchild != - && first == )
cout << "(";
inOrder(tree[root].lchild, );
cout << ss;
inOrder(tree[root].rchild, );
if(tree[root].rchild != - && first == )
cout << ")";
}
int main(){
scanf("%d", &N);
for(int i = ; i <= N; i++){
int cl, cr;
string ss;
cin >> ss >> cl >> cr;
tree[i].lchild = cl;
tree[i].rchild = cr;
tree[i].data = ss;
if(cl != -)
notRoot[cl] = ;
if(cr != -)
notRoot[cr] = ;
}
int root;
for(root = ; root <= N && notRoot[root] == ; root++);
inOrder(root, );
cin >> N;
return ;
}

总结:

1、中序遍历即可,当该节点不是叶节点时,则需要先输出" ( ", 再递归访问左子树,右括号同理。起初我是用判断 + - * %来判断是否是非叶节点,结果最后一个测试点过不去。后来改成判断子树是否为空就全过了。这里可能是由于运算符号不止有+-*%,可能还有未知的运算符号。

A1130. Infix Expression的更多相关文章

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

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

  2. PAT甲级——A1130 Infix Expression【25】

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

  3. PAT_A1130#Infix Expression

    Source: PAT A1130 Infix Expression (25 分) Description: Given a syntax tree (binary), you are suppose ...

  4. PAT1130:Infix Expression

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

  5. PAT 甲级 1130 Infix Expression

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

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

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

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

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

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

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

  9. PAT 1130 Infix Expression

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

随机推荐

  1. python爬虫之MongoDB测试环境安装

    一.   下载 从http://www.mongodb.org/downloads地址中下载:mongodb-linux-x86_64-2.4.11.tar 二.  安装 1>设置mongoDB ...

  2. Mybatis核心配置文件SqlMapConfig.xml

    配置内容: SqlMapConfig.xml中配置的内容和顺序如下: 1.properties(属性) 2.settings(全局配置参数) 3.typeAliases(类型别名) 4.typeHan ...

  3. tomcat 和jboss区别

    参考http://blog.csdn.net/sz_bdqn/article/details/6762175

  4. MySQL函数GROUP_CONCAT

    该函数返回带有来自一个组的连接的非NULL值的字符串结果.该函数是一个增强的Sybase SQL Anywhere支持的基本LIST()函数. 语法结构: GROUP_CONCAT([DISTINCT ...

  5. Jenkins+PowerShell持续集成环境搭建(二)控制台项目

    1. 新建一个名字为HelloWorld.Console的Freesyle项目: 2. 配置源码管理: 3. 编译配置: 版本:选择MSBuild4 文件:D:\CI\Config\HelloWorl ...

  6. mysql 集群方案

    试试基于Galera的MySQL高可用集群  mha  mgr

  7. BZOJ2428[HAOI2006]均分数据——模拟退火

    题目描述 已知N个正整数:A1.A2.…….An .今要将它们分成M组,使得各组数据的数值和最平均,即各组的均方差最小.均方差公式如下: ,其中σ为均方差,是各组数据和的平均值,xi为第i组数据的数值 ...

  8. Codeforces Round #470 Div. 1

    A:暴力枚举x2的因子,由此暴力枚举x1,显然此时减去其最大质因子并+1即为最小x0. #include<iostream> #include<cstdio> #include ...

  9. Sublime Text3 如何开启Debug

    打开setting-user 首选项——>Package Settings——>Package Control——>settings-user 添加"debug" ...

  10. P1200 你的飞碟在这儿

    P1200 题目描述 众所周知,在每一个彗星后都有一只UFO.这些UFO时常来收集地球上的忠诚支持者.不幸的是,他们的飞碟每次出行都只能带上一组支持者. 因此,他们要用一种聪明的方案让这些小组提前知道 ...