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. 如何理解c++迭代器(上)

    1.如何理解迭代器?迭代器不是指针,也似乎不是string这种类型 参考:迭代器与指针的区别是? C++map迭代器的++操作是如何实现的?讨论.iterator提供了遍历STL容器里元素的方式,no ...

  2. s11 day106-107 RBAC模块

    一.登录 把权限存在session中 1. rbac models from django.db import models class Permission(models.Model): " ...

  3. [题解]Magic Line-计算几何(2019牛客多校第三场H题)

    题目链接:https://ac.nowcoder.com/acm/contest/883/H 题意: 给你偶数个点的坐标,找出一条直线将这n个点分成数量相等的两部分 并在这条直线上取不同的两个点,表示 ...

  4. tfsenflow队列|tf.train.slice_input_producer|tf.train.Coordinator|tf.train.start_queue_runners

      #### ''' tf.train.slice_input_producer :定义样本放入文件名队列的方式[迭代次数,是否乱序],但此时文件名队列还没有真正写入数据 slice_input_pr ...

  5. smartforms 字段文本碰见 "-" 自动换行

    长文本会在 '-' 这个符号处自动换行 原理:SAP 标准SMARTFORMS 的功能,遇到 '-' 自动判断后面字段是否能在本行完全显示,不够则换行 注意:如果一行文本有多个 ‘-’ ,则 判断 ' ...

  6. 两种图片延迟加载的方法总结jquery.scrollLoading.js与jquery.lazyload.js---转载

    jquery.scrollLoading方法 html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml& ...

  7. SnowFlake --- 分布式id生成算法

    转载自:https://segmentfault.com/a/1190000011282426 概述 SnowFlake算法生成id的结果是一个64bit大小的整数,它的结构如下图: 1位,不用.二进 ...

  8. centos7.3安装wordpress

    一.安装并配置数据库 1.安装mariadb #yum install -y mariadb-server mariadb 2.启动数据库并设置开机自启#systemctl start mariadb ...

  9. (转)收集 Spring Boot 相关的学习资料,Spring Cloud点这里 重点推荐:Spring Boot 中文索引

    推荐博客 纯洁的微笑-Spring Boot系列文章 林祥纤-从零开始学Spring Boot Mkyong-Spring Boot教程(国外) baeldung-Spring Boot教程(国外) ...

  10. 如何判断一个List集合中是否有空值

    list.Any(v => string.IsNullOrWhiteSpace(v))