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. win10查看无线密码

  2. 设计模式之原型模式(c++)

    问题描述 看到这个模式,很容易想到小时候看的<西游记>,齐天大圣孙悟空发飙的时候可以通过自己头上的 3 根毛立马复制出来成千上万的孙悟空, 对付小妖怪很管用(数量最重要). Prototy ...

  3. java 中Excel的导入导出

    部分转发原作者https://www.cnblogs.com/qdhxhz/p/8137282.html雨点的名字  的内容 java代码中的导入导出 首先在d盘创建一个xlsx文件,然后再进行一系列 ...

  4. python[练习题]:实现Base64编码

    要求自己实现算法,不用库. Base64简介: Base64是一种用64个字符来表示任意二进制数据的方法. 用记事本打开exe.jpg.pdf这些文件时,我们都会看到一大堆乱码,因为二进制文件包含很多 ...

  5. netstat -na 查看有大量TIME_WAIT解决办法(修改内核参数)

    # netstat -an|awk '/tcp/ {print $6}'|sort|uniq -c      16 CLOSING     130 ESTABLISHED     298 FIN_WA ...

  6. linux通过命令行查看MySQL编码并修改-简洁版方法

    云服务器环境:CentOS 7.4 因为服务器配置较低,故使用MySQL5.5 未进行设置前 1.查看字符编码: mysql> show variables like '%character%' ...

  7. npm 设置代理

    设置代理 npm config set proxy http://username:password@server:portnpm config set https-proxy http://user ...

  8. Java的hashCode和equals方法

    当然健壮的代码,两个都重写那是最好. 用不到hashCode的, 也没有必要重写hashCode. 个人感觉. 哈希机制的Java集合类,例如 Hashtable, HashMap, HashSet ...

  9. Windows 10 安装PHP Manager 失败的解决办法

    首先安装.NET 2.0和.NET 3.5, 在  控制面板----程序----启用或关闭Windows功能   里面 然后修改注册表:HKLM/System/CCS/Services/W3SVC/P ...

  10. Vue-router的API详解

    前面的话 本文将详细介绍Vue-router的API router-link <router-link> 组件支持用户在具有路由功能的应用中点击导航. 通过 to 属性指定目标地址,默认渲 ...