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. centos7下安装docker(12.1bridge网络)

    容器默认使用的时bridge网络 docker安装时会创建一个 命令为docker0的linux bridge.如果不指定--network=,运行的容器会默认挂到docker0上 interface ...

  2. go标准库的学习-net/rpc

    参考:https://studygolang.com/pkgdoc 导入方法: import "net/rpc" RPC(Remote Procedure Call Protoco ...

  3. Qt 打包发布程序

    利用Qt Creator写好程序,选择对应的编译器编译程序. 编译完成会在项目同级目录生成对应的目录来保存编译后的输出. 打包程序就要选择Qt自带的CMD工具,分别有下面几种. 比如,打包VS2017 ...

  4. 【Codeforces 1129C】Morse Code

    Codeforces 1129 C 题意:给一个0/1串,问它的每一个前缀中的每一个子串能解析成莫尔斯电码的串的种数. 思路:首先对于这个串构造后缀自动机,那么从起点走到每一个节点的每一条路径都代表了 ...

  5. BZOJ3720 Gty的妹子树 询问分块、主席树

    传送门 学到了询问分块的科技-- 对于修改操作,每发生了\(S\)次修改就重构整棵树,小于\(S\)次的修改操作丢到一个队列里面. 对于每一次查询操作,先在主席树上查询当前子树内部大于\(k\)的节点 ...

  6. JavaWeb开发中采用FreeMarker生成Excel表格

            最近做了一个需求,要求导出一个采购合同的Excel表格,这个表格样式比较多.由于是合同,这个Excel表格里面有好多格式要求,比如结尾处签字那部分就有格式要求.这里介绍种采用FreeM ...

  7. Ionic项目的建立

    Ionic建立android项目的过程 1.cmd到目标盘文件,此处为D:\Dev\sourcecode\IonicApp\FlexApp\CaseStudy,执行ionic start CaseSt ...

  8. [Spark][Python]DataFrame select 操作例子

    [Spark][Python]DataFrame中取出有限个记录的例子 的 继续 In [4]: peopleDF.select("age")Out[4]: DataFrame[a ...

  9. DefWindowProc是一个会产生消息的函数

    先看一道题目: 当用户点击右上角关闭按钮的时候,请给下列Windows做出的响应排个序:A:发送 WM_QUIT 消息     B:发送 WM_CLOSE 消息     C:发送 WM_DESTROY ...

  10. mysql 常用的几个函数

    IF 函数 语法:`IF`(expr1,expr2,expr3); 当expr1为ture时,值为expr2,当expr1为false时,值为expr3. 如: IFNULL 函数 语法:IFNULL ...