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. eclipse+tomcat测试连接时候HTTP Status 404错误

    想要在eclipse里部署tomcat,结果tomcat单独可以通过连接测试,用eclipse就404了 404肯定都是目录不对,试了半天在eclipse下改了一下配置和文件位置就行了 1.先在菜单栏 ...

  2. javascript高级选择器querySelector和querySelectorAll

    querySelector 和 querySelectorAll 方法是 W3C Selectors API规范中定义的.他们的作用是根据 CSS 选择器规范,便捷定位文档中指定元素. 目前几乎主流浏 ...

  3. Kmeans基本思想

    https://blog.csdn.net/zjc_game_coder/article/details/78595833 Kmeans算法的基本思想:看如下图: 解决小样本 .非线性及高维模式识别问 ...

  4. 【Codeforces 1105E】Helping Hiasat

    Codeforces 1105 E 题意:给你m个事件,每个事件可能是以下两种之一: \(1\),代表此时可以更改用户名 \(2\) \(s\),代表\(s\)来查看是否用户名与其名字相符 一共有\( ...

  5. Ubuntu16.04中搭建TFTP 和 NFS 服务器

    Ubuntu 16.04中搭建TFTP服务 1. 安装 $ apt-get install tftp-hpa tftpd-hpa   2. 建立目录 $ mkdir /tftpboot # 这是建立t ...

  6. eclipse调试断点【转载】

    该片博文是转载他人的博客,原博客地址:http://blog.csdn.net/maritimesun/article/details/7815903 作为开发人员,掌握开发环境下的调试技巧十分有必要 ...

  7. 如何传递参数给ASP.NET Core的中间件(Middleware)

    问题描述 当我们在ASP.NET Core中定义和使用中间件(Middleware)的时候,有什么好的办法可以给中间件传参数吗? 解决方案 在ASP.NET Core项目中添加一个POCO类来传递参数 ...

  8. C#搭建CEF(CEFGLUE) 环境。

    CEF(CEFGLUE)如果想做浏览器的,对这个应该不陌生了,相关资料执行百度了,现在写这文章这是按当前时间做一个环境搭建时所需要的资料的一个收集. 1:下载Xilium.CefGlue项目源码. 链 ...

  9. WPF 实现主从的datagrid以及操作rowdetailtemplate 的方法

    原文:WPF 实现主从的datagrid以及操作rowdetailtemplate 的方法 WPF 实现主从的datagrid以及操作rowdetailtemplate 的方法        最近在做 ...

  10. vue2.0中使用sass

    第一部分:Sass语言 Sass是一种强大的css扩展语言(css本身并不是一门语言),它允许你使用变量.嵌套规则.mixins.导入等css没有但开发语言(如Java.C#.Ruby等)有的一些特性 ...