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. vue的地图插件amap

    https://www.jianshu.com/p/0011996b81e2(amap) npm install vue-amap --save

  2. 你不知道的JavaScript——第二章:this全面解析

    1调用位置 调用栈:为了到达当前执行位置所调用的所有函数. function baz(){ //当前调用栈:baz //因此,当前调用位置是全局作用域 console.log('baz'); bar( ...

  3. Python基础知识2-内置数据结构(下)

    bytes.bytearray #思考下面例子: a = 1 b = a print(a == b)#True print(a is b)#True print(id(a) is id(b))#Fal ...

  4. php 将数组转换网址URL参数

    $array =array ( 'id' =123, 'name' = 'dopost' );echo http_build_query( $array );//得到结果id=123name=dopo ...

  5. 使用阿里云OSS,上传图片时报错:java.lang.ClassNotFoundException:org.apache.http.ssl.TrustStrategy

    问题产生的原因就是jar包版本问题,阿里的SDk引入的pom中依赖的httpclient和httpcore版本高于当前项目中已经设置的版本. 解决: 删除低版本后,更新下项目.

  6. 设计模式笔记:开闭原则(OCP,The Open-Closed Principle)

    1. 开闭原则概述 开闭原则(OCP,The Open-Closed Principle)两个主要特征: (1)对扩展开放(open for extension):模块的行为的可以扩展的,当应用的需求 ...

  7. X5中CSS设置

    颜色渐变 position:absolute;left:0;top:40%; 效果图 点击导航按钮变化颜色 1.设置按钮class为 btn-link(超链接) 2.为每一个导航按钮增加属性id 3. ...

  8. PHPStorm从入门到精通

    1. 使用phpstorm+xdebug进行调试 首先,安装php的xdebug扩展 查看phpinfo中php的版本,php的安装位数,php的是否线程安全:根据这些下载对应的xdebug.dll ...

  9. ADO.NET工具类(二)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  10. PlaNet,使用图像输入来学习世界模型

    Google AI团队与DeepMind合作,上周宣布了一个名为PlaNet的新的开源“Deep Planning”网络. PlaNet是一个人工智能代理,它只使用图像输入来学习世界模型,并使用这些模 ...