https://pintia.cn/problem-sets/994805342720868352/problems/994805347921805312

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 −. 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 <bits/stdc++.h>
using namespace std; int N;
int vis[30]; struct Node{
string val;
int L, R;
}node[30]; string dfs(int st) {
if(node[st].L == -1 && node[st].R == -1) return node[st].val;
if(node[st].L == -1 && node[st].R != -1) return "(" + node[st].val + dfs(node[st].R) + ")";
if(node[st].L != -1 && node[st].R != -1) return "(" + dfs(node[st].L) + node[st].val + dfs(node[st].R) + ")";
} int main() {
scanf("%d", &N);
for(int i = 1; i <= N; i ++) {
cin >> node[i].val >> node[i].L >> node[i].R;
if(node[i].L != -1) vis[node[i].L] = 1;
if(node[i].R != -1) vis[node[i].R] = 1;
} int root = 1;
while(vis[root] == 1) root ++;
string ans = dfs(root);
if(ans[0] == '(') ans = ans.substr(1, ans.size() - 2);
cout << ans;
return 0;
}

  dfs 递归 最后去掉最外面的括号

可能最近是自闭 girl 了 希望有好运气叭

PAT 甲级 1130 Infix Expression的更多相关文章

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

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

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

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

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

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

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

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

  5. PAT 1130 Infix Expression

    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. PAT甲级题解(慢慢刷中)

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

随机推荐

  1. 使用vue-cli脚手架创建的项目结构详解

    项目整体目录结构预览 src目录 src整体结构 开发过程中基本上操作都在该目录下进行操作的,项目所有源码都是在这个目录下 main.js文件,项目核心文件 App.vue文件,项目入口文件 rout ...

  2. Properties集合_修改配置信息

    集合中的数据来自于一个文件  注意:必须要保证该文件中的数据是键值对.  需要使用到读取流 使用load()方法读取已有文件中的数据,存储到Properties集合中 public class Pro ...

  3. mac下更改Jupyter notebook工作目录

    Jupyter notebook运行之后,默认的工作目录在mac下是个人文件夹,在windows下貌似也是如此.显然不太合理,需要修改它. 具体办法是: 进入终端命令行模式,输入下面的代码: jupy ...

  4. vue.js 传参 href传参 与router-link传参

    每天学习一点点 编程PDF电子书免费下载: http://www.shitanlife.com/code 1. <a v-bind:href="'#/appinfo/'+cateApp ...

  5. linked-list-cycle (快慢指针判断是否有环)

    class Solution { public: bool hasCycle(ListNode *head) { if (head == NULL) return NULL; //空表 ListNod ...

  6. MySql常用命令集Mysql常用命令4

    说明: 用中括号([])括起来的部分表示是可选的,用大括号({})括起来的部分是表示必须 从中选择其中的一个. 1 FROM子句 FROM 子句指定了Select语句中字段的来源.FROM子句后面是包 ...

  7. win7重装系统

    读了四年大学的计算机专业,说自己不会重装系统真的会笑掉大牙,但人家是女生,怕东怕西的,害怕弄坏自己的电脑,毕竟上计算机课最重要的是电脑,嘿嘿,其实是为了开wifi和看电视剧.今天终于有一台闲置的计算机 ...

  8. Objective-C KVO深入理解

    KVO(Key Value Observing,键值观察),是Objective-C观察者模式的实现.当被观察对象的某个属性发生变化时,观察对象就会收到通知. 实现原理: 1)在运行期,为被观察对象的 ...

  9. 【UOJ 17】飞扬的小鸟

    UOJ 17 题意:在\(n\times m\)的网格中有一些柱子,它们可以通过的区间是\((L_i,R_i)\),位置在\(P_i\).在第i个位置点击一次会使高度增加\(X_i\),不点击会使高度 ...

  10. C语言程序设计II—第九周教学

    第九周教学总结(22/4-28/4) 教学内容 本周的教学内容为: 9.1 输出平均分最高的学生信息 知识点:结构的概念.结构的定义形式.结构的嵌套定义.结构变量和结构成员变量的引用.重难点:结构变量 ...