PAT 1130 Infix Expression[难][dfs]
1130 Infix Expression (25 分)
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 −1. 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表示没有子节,要求输出对应的中缀树,并且对这个运算顺序加上括号。
//输出中缀树就比较简单了,但是这个需要加上括号,我是真的不会。好难啊。
//看了柳神的解答,学习了!



1.学习了dfs,这个对我来说一直都很难。
2.怎样去找root。使用标记。
PAT 1130 Infix Expression[难][dfs]的更多相关文章
- PAT 1130 Infix Expression
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- PAT甲级 1130. Infix Expression (25)
1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...
- PAT甲级——1130 Infix Expression (25 分)
1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...
- PAT 甲级 1130 Infix Expression
https://pintia.cn/problem-sets/994805342720868352/problems/994805347921805312 Given a syntax tree (b ...
- PAT A1130 Infix Expression (25 分)——中序遍历
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- 1130. Infix Expression (25)
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- PAT甲题题解-1130. Infix Expression (25)-中序遍历
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789828.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 1130 Infix Expression
题意:给出一个语法树(二叉树),输出相应的中缀表达式. 思路:很显然,通过中序遍历来做.通过观察,发现除了根结点之外的所有非叶结点的两侧都要输出括号,故在中序遍历时判断一下即可. 代码: #inclu ...
- PAT1130:Infix Expression
1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...
随机推荐
- 数学 - SGU 118. Digital Root
Digital Root Problem's Link Mean: 定义f(n)为n各位数字之和,如果n是各位数,则n个数根是f(n),否则为f(n)的数根. 现在给出n个Ai,求出A1*A2*…*A ...
- javascript 实现java中的Map
javascript实现java中的Map,代码是在国外的一个网站上看到的(http://stackoverflow.com/questions/368280/javascript-hashmap-e ...
- java开发总体知识复习
上一篇发了一个找工作的面经, 找工作不宜, 希望这一篇的内容能够帮助到大家. 对于这次跳槽找工作, 我准备了挺长的时间, 其中也收集了很多比较好的笔试面试题, 大都是一些常用的基础, 很多都是由于时间 ...
- 嵌入式开发之davinci--- 8148/8168/8127 中的二维图像处理内存tiler 铺瓷砖
http://blog.csdn.net/shanghaiqianlun/article/details/7619603
- SpringCloud服务发现(Eureka)简介
Eureka是Netflix开发的服务发现框架,SpringCloud将它集成在自己的子项目spring-cloud-netflix中,实现SpringCloud的服务发现功能. 为什么要使用Eure ...
- [SDOI2016 Round1] 征途[斜率优化]
2225. [SDOI2016 Round1] 征途 ★★★☆ 输入文件:menci_journey.in 输出文件:menci_journey.out 简单对比时间限制:1 s 内存 ...
- 【BZOJ1495】[NOI2006]网络收费 暴力+DP
[BZOJ1495][NOI2006]网络收费 Description 网络已经成为当今世界不可或缺的一部分.每天都有数以亿计的人使用网络进行学习.科研.娱乐等活动.然而,不可忽视的一点就是网络本身有 ...
- 【BZOJ4518】[Sdoi2016]征途 斜率优化
[BZOJ4518][Sdoi2016]征途 Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除 ...
- Objective-C代码学习大纲(2)
2011-05-11 14:06 佚名 otierney 字号:T | T 本文为台湾出版的<Objective-C学习大纲>的翻译文档,系统介绍了Objective-C代码,很多名词为台 ...
- angular 封装公共方法
angular封装公共方法到service中间件,节省开发时间 layer.service.ts openAlert(callback) {// 传递回调函数 const dialogRef = th ...