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))
题⽬⼤意:给⼀个⼆叉树,输出中缀表达式,且加上括号表示运算的优先级~
分析:⾸先根据所有孩⼦结点编号寻找1~n中没有出现过的编号标记为root,即树的根结点~然后进
⾏从root结点开始dfs~
dfs递归拼接 “(” + 左⼦树 + 根 + 右⼦树 + “)” 递归有四种情况(有效的只有三种):
1. 左右⼦树都空 返回 “(” + 根 + “)”
2. 左空右不空 返回 “(” + 根 + 右⼦树 + “)”
3. 左不空右空 这种情况不存在
4. 左右都不空 返回 “(” + 左⼦树 + 根 + 右⼦树 + “)” 最后递归返回的ans,最外层可能会被括号包起
来,也可能不被包起来。要判断⼀下,如果被包起来,把最外层括号去掉即可~

 #include <iostream>
#include <vector>
#include <string>
using namespace std;
struct node
{
string str;
int l, r;
}node[];
int n;
string dfs(int root)
{
if (node[root].l == - && node[root].r == -)
return node[root].str;
if (node[root].l == - && node[root].r != -)
return "(" + node[root].str + dfs(node[root].r)+")";
if (node[root].l != - && node[root].r != -)
return "(" + dfs(node[root].l) + node[root].str + dfs(node[root].r) + ")";
}
int main()
{
cin >> n;
int isroot[] = { }, root = ;
for (int i = ; i <= n; ++i)
{
cin >> node[i].str >> node[i].l >> node[i].r;
isroot[node[i].l == - ? : node[i].l] = ;
isroot[node[i].r == - ? : node[i].r] = ;
}
while (isroot[root] == )root++;//找出根节点,即根节点不是任何节点的孩子节点
string res = dfs(root);
if (res[] == '(')res = res.substr(, res.length() - );//去除最外面的一层括号
cout << res << endl;
return ;
}

PAT甲级——A1130 Infix Expression【25】的更多相关文章

  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 甲级 1130 Infix Expression

    https://pintia.cn/problem-sets/994805342720868352/problems/994805347921805312 Given a syntax tree (b ...

  4. PAT A1130 Infix Expression (25 分)——中序遍历

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

  5. A1130. 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甲级】1070 Mooncake (25 分)(贪心水中水)

    题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...

  8. PAT甲题题解-1130. Infix Expression (25)-中序遍历

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

  9. PAT甲级 1122. Hamiltonian Cycle (25)

    1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

随机推荐

  1. LightOJ 1418 Trees on My Island (Pick定理)

    题目链接:LightOJ 1418 Problem Description I have bought an island where I want to plant trees in rows an ...

  2. python 装饰器 第五步(1):带有参数的装饰器

    #第五步:带有参数的装饰器 #用于扩展基本函数的函数 def kuozhan(func): #内部函数(扩展之后的eat函数) #5由于调用的时候传了两个参数,未来的eat函数没有参数接收 #5报错的 ...

  3. CSS深入理解float

    初衷:图片环绕效果 1.会使父元素高度塌陷 2.包裹性 3.让元素block化 4.去空格化   5.清除浮动 .clearfix::after{ content:""; disp ...

  4. linux中的常用信号

    linux中的常用信号,见如下列表: 信号名 值 标注 解释 ------------------------------------------------------------------ HU ...

  5. 三、IIS通过目录方式部署以供外部调试

    一.IIS 下面是通过 gif 为 因项目是bin生成后的,非运行方式的调试,所以断点调试无效,仅修改文件后,右击项目重新生成解决方案即可,好处:启动快,坏处:不可以断点调试查看变量和分步执行语句.

  6. Java中File的处理

    不知道“文件”和“文件路径”是否存在的处理情况 1.如果是文件,先获取文件父路径,没有就生成父路径,然后再生成文件. public class TestMain { public static voi ...

  7. c++while控制语句

    while语句结构:while(condition){ statement; } condition 表示返回值是true or false 如果返回的一直是true则statement语句则一直执行 ...

  8. Struts2中Action类的三种写法

      一.普通的POJO类(没有继承没有实现)-基本不使用 POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创 ...

  9. jquery 下拉框左右选择

    html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...

  10. java 调用wsdl的webservice接口 两种调用方式

    关于wsdl接口对于我来说是比较头疼的 基本没搞过.一脸懵 就在网上搜 看着写的都很好到我这就不好使了,非常蓝瘦.谨以此随笔纪念我这半个月踩过的坑... 背景:短短两周除了普通开发外我就接到了两个we ...