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】的更多相关文章

  1. PAT甲级【2019年3月考题】——A1157 Anniversary【25】

    Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for the celebrat ...

  2. PAT甲级【2019年9月考题】——A1164 DijkstraSequence【30】

    7-4 Dijkstra Sequence (30 分) Dijkstra's algorithm is one of the very famous greedy algorithms. It is ...

  3. 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 ...

  4. PAT甲级【2019年9月考题】——A1160 Forever【20】

    7-1 Forever (20 分) "Forever number" is a positive integer A with K digits, satisfying the ...

  5. 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 ...

  6. PAT甲级【2019年3月考题】——A1158 TelefraudDetection【25】

    Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting ...

  7. 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 ...

  8. PAT甲级2019冬季考试题解

    A Good In C纯模拟题,用string数组读入数据,注意单词数量的判断 #include<bits/stdc++.h> using namespace std; ; ][]; in ...

  9. PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作

    给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. ...

随机推荐

  1. Python入门习题5.蒙特卡罗方法计算圆周率

    #CalPi.py from random import random from math import sqrt from time import clock DARTS = 10000000 hi ...

  2. 洛谷 P3374 【模板】树状数组 1(单点加,区间和)

    题目链接 https://www.luogu.org/problemnew/show/P3374 树状数组 树状数组最基本的就是求区间和. 维护: 空间复杂度:O(n) 时间复杂度(区间和,单点修改) ...

  3. D Dandan's lunch

    链接:https://ac.nowcoder.com/acm/contest/338/D来源:牛客网 题目描述 As everyone knows, there are now n people pa ...

  4. dubbo构建应用

    1.Dubbo介绍 Dubbo是 阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成. 2.Dubbo原理 是不是看着 ...

  5. EAN13条码的校验位的Excel算法

    校验位原公式: 单元格=10-RIGHT(SUM(MID($B3,{1;2;3;4;5;6;7;8;9;10;11;12},1)*{1;3;1;3;1;3;1;3;1;3;1;3})) 简化公式: 单 ...

  6. linux的svn服务器搭建--Subversion Edge

    linux下的collabnetsubversionedge的安装: 安装条件(运行环境) jdk + python + httpd 1.root用户下建立svnroot用户,及设定密码 userad ...

  7. windows linux子系统(Windows Subsystem for Linux)的存放目录

    win10子系统把windows的底层接口做了个转换到Linux从而能运行linux,但是他在安装的时候并没有提供安装位置的选项.(还有hyper v) 现在,所有从商店安装的发行版都存在于以下目录中 ...

  8. RBAC用户权限管理数据库设计的图文详解

    RBAC(Role-Based Access Control,基于角色的访问控制),就是用户通过角色与权限进行关联.简单地说,一个用户拥有若干角色,每一个角色拥有若干权限.这样,就构造成“用户-角色- ...

  9. webRTC脱坑笔记(一)— 初识webRTC

    webRTC概述 WebRTC--- `Web browsers with Real-Time Communications (RTC)` WebRTC是一个开源项目,可以在`Web`和本机应用程序中 ...

  10. Python的pip源切换为国内阿里云镜像

    Python的pip源切换为国内阿里云镜像 找到用户目录 C:\Users\用户\pip,如果不存在就新建该文件夹. 新建文件pip.ini,并用文本编辑器输入以下内容并保存 [global] ind ...