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. 【洛谷UVA307】小木棍Sticks

    小木棍Sticks[传送门] 算法的话:dfs+超强剪枝: (另外注意UVA上好像不接受万能头[因为万能头WA了两次,瑟瑟发抖]) 思路: 最直接的思路,枚举木棍长度来dfs,但这样很容易就TLE了. ...

  2. HDU 5441 Travel (离线dsu)

    <题目链接> 题目大意:$n$个点,$m$条边,每条边具有对应的权值,然后进行$k$次询问,每次询问给定一个值,所有权值小于等于这个的边所对应的点能够相连,问每次询问,这些能够相互到达的点 ...

  3. NGUI的widget的使用

    一,我们看看widget有什么属性,如下图: 二,Pivot是什么意思? 我们都知道在Untiy3D中有一个中央坐标点,而这个Pivot这个就是选择控件的某一个点与中央坐标点定位. 如下图区别: 当你 ...

  4. js IntersectionObserver api

    API const options = { root: null, threshold: [0, 0.5, 1], rootMargin: '30px 100px 20px' } var io = n ...

  5. ES6——函数-箭头函数

    箭头函数: 1.普通函数 function 函数名(){...} 2.箭头函数 注意:  1)如果只有一个返回值,{}return可以省略: let arr = [12,5,8,99,33,14,26 ...

  6. 2018-2-13-WPF-绑定密码

    title author date CreateTime categories WPF 绑定密码 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23:3 + ...

  7. Kotlin搞起来——2.基础知识

    在上一节中简单的给大家介绍了下Kotlin的特点,以及结合自己实际项目 中的使用来帮助大家了解这门语言,其实真的没你想象中的那么难,本文打算 介绍的是Kotlin中基础相关的一些语法(用法),有个大概 ...

  8. Linux设备驱动详解 宋宝华 硬件基础

    处理器 存储器 接口与总线 I2C时序 SPI总线时序 以太网

  9. python-字符串的处理

    s1 = '###12314##231###' print(s1.split('#')) #split,从左往右遇见# 就拆分一次['', '', '', '12314', '', '231', '' ...

  10. golang-练习2

    反转字符串 package main import "fmt" func FirstReverse(str string) string { var str1 []rune run ...