PAT甲级【2019年9月考题】——A1163 PostfixExpression【25】
7-3 Postfix Expression (25 分)
Given a syntax tree (binary), you are supposed to output the corresponding postfix 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) 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.


Output Specification
For each case, print in a line the postfix expression, with parentheses reflecting the precedences of the operators.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)%))+)
【声明】
由于此题还未上PAT官网题库,故没有测试集,仅仅是通过了样例,若发现错误,感谢留言指正。
Solution:
这道题与A1130 Infix Expression如出一辙,只不过换了一下位置。
开始我重建了二叉树,后发现根本不用,因为给出的信息就是一个树的形状
使用DFS遍历,先左,再右,后节点,
唯一注意的就是缺少左孩子节点的情况【不可能只会出现缺少右孩子节点的情况,不然就不是等式】
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int n, head = -;
struct Node
{
string val;
int l, r;
}nodes[];
string DFS(int root)
{
if (root == -) return "";
if (nodes[root].l == -)//记住,只允许左子树为空,不能右子树为空,不然就不是个算式
return "(" + nodes[root].val + DFS(nodes[root].r) + ")";
else
return "(" + DFS(nodes[root].l) + DFS(nodes[root].r) + nodes[root].val + ")";
}
int main()
{
cin >> n;
vector<bool>isHead(n + , true);//找到头节点
for (int i = ; i <= n; ++i)
{
string a;
int b, c;
cin >> a >> b >> c;
nodes[i] = { a,b,c };
isHead[(b == - ? : b)] = isHead[(c == - ? : c)] = false;
}
for (int i = ; i <= n && head == -; ++i)//找到头节点
if (isHead[i])head = i;
string res = DFS(head);
cout << res;
return ;
}
PAT甲级【2019年9月考题】——A1163 PostfixExpression【25】的更多相关文章
- PAT甲级【2019年3月考题】——A1157 Anniversary【25】
Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for the celebrat ...
- PAT甲级【2019年9月考题】——A1164 DijkstraSequence【30】
7-4 Dijkstra Sequence (30 分) Dijkstra's algorithm is one of the very famous greedy algorithms. It is ...
- PAT甲级【2019年9月考题】——A1162 MergingLinkedLists【25】
7-2 Merging Linked Lists (25 分) Given two singly linked lists L 1 =a 1 →a 2 →...→a n−1 →a n L1=a1→a ...
- PAT甲级【2019年9月考题】——A1160 Forever【20】
7-1 Forever (20 分) "Forever number" is a positive integer A with K digits, satisfying the ...
- PAT甲级【2019年3月考题】——A1159 Structure_of_a_BinaryTree【30】
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- PAT甲级【2019年3月考题】——A1158 TelefraudDetection【25】
Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting ...
- PAT甲级【2019年3月考题】——A1156 SexyPrimes【20】
Sexy primes are pairs of primes of the form (p, p+6), so-named since “sex” is the Latin word for “si ...
- PAT甲级2019冬季考试题解
A Good In C纯模拟题,用string数组读入数据,注意单词数量的判断 #include<bits/stdc++.h> using namespace std; ; ][]; in ...
- PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作
给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. ...
随机推荐
- k8s入门教程
1. k8s概述 Kubernetes(简称K8S) 是Google开源的分布式的容器管理平台,方便我们在服务器集群中管理我们容器化应用. 教程主要介绍怎么使用阿里云容器服务(kubernetes版本 ...
- asp.net 连续打印多份HTML页面,出现漏字
在IE8上打印的内容用了CSS 样式,连续打印多份导致的 可以通过改变CSS样式或更换浏览器解决
- NGUI的sprite的使用(九宫切图)
一,图集的添加和背景图的选择 图集的选择Atlas和背景图的选择Sprite 二,type的sliced切背景图的使用 当选择Sprite后的edit时,我们就可以设置当前背景图的边距,也有切图的意思 ...
- C#设计模式:单例模式(Singleton)
一,单例模式:它的主要特点不是根据客户程序调用生成一个新的实例,而是控制某个类型的实例数量-唯一一个,就是保证在整个应用程序的生命周期中,在任何时刻,被指定的类只有一个实例,并为客户程序提供一个获取该 ...
- BUUCTF--reverse2
测试文件:https://buuoj.cn/files/ef0881fc76e5bcd756b554874ef99bec/e8722e94-93d7-45d5-aa06-a7aa26ce01a1.ra ...
- idea 2019.1.3最新注册码
CATF44LT7C-eyJsaWNlbnNlSWQiOiJDQVRGNDRMVDdDIiwibGljZW5zZWVOYW1lIjoiVmxhZGlzbGF2IEtvdmFsZW5rbyIsImFzc ...
- 通用的规则匹配算法(原创)(java+.net)
1.java里可以使用Spring的 Spel或者Google的Aviator 如果使用 Aviator 则添加以下依赖 <dependency> <groupId>com.g ...
- linux ---apache的安装和配置
linux环境下的安装:yum安装和tar包安装 yum安装: 首先安装php环境 yum install php55w yum install php55w-mysql yum install ph ...
- easyapi
create database easyrec; #为easyrec初始化用户名跟密码grant index, create, select, insert, update, drop, delete ...
- 【串线篇】Mybatis缓存之二级缓存
1.应用 二级缓存:namespace级别的缓存:SqlSession关闭或者提交以后有效 一级缓存:SqlSession关闭或者提交以后,一级缓存的数据会放在二级缓存中: 二级缓存的使用:mybat ...