当前位置:网站首页>Pat class a 1162 postfix expression
Pat class a 1162 postfix expression
2022-07-05 02:46:00 【Handsome BIA】
1162 Postfix Expression (25 branch )
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) 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)%))+)
Thought analysis : This topic and 1130 similar , Are given an expression tree to find his expression , This topic requires finding the suffix expression , Then the idea is very clear , We just need to build a tree , Then it is OK to print after traversal , But pay attention to , If there is a node without a left child , Then we have to print the root node first , Here we use a st Array tags are ok . One last word , Input methods like this , We use two-dimensional arrays tree[N][2] Building trees can be more convenient , The second is to store the location of the two children , Use another w[N] Stored value
#include<bits/stdc++.h>
using namespace std;
const int N = 25;
int tree[N][2];
string w[N],value;
bool st[N];
int n,l,r,root;
void print(int t)
{
if(!st[t]) cout<<w[t];
st[t] = 1;
}
void dfs(int t)
{
if (t == -1) return ;
printf("(");
if(tree[t][0] == -1) print(t);
dfs(tree[t][0]);
dfs(tree[t][1]);
print(t);
printf(")");
}
int main()
{
cin >> n;
for (int i = 1 ;i <= n ; i ++ )
{
cin >> value >> l >> r;
tree[i][0] = l,tree[i][1] = r;
st[l] = 1,st[r] = 1;
w[i] = value;
}
for (int i = 1; i <= n; i ++ ) if(!st[i]) root = i;
memset(st,0,sizeof st);
dfs(root);
return 0;
}
There is probably so much content , I hope I can help you .
边栏推荐
- Devtools的简单使用
- Eight days of learning C language - while loop (embedded) (single chip microcomputer)
- 使用druid連接MySQL數據庫報類型錯誤
- Pytest (4) - test case execution sequence
- tuple and point
- Hmi-32- [motion mode] add light panel and basic information column
- When the low alcohol race track enters the reshuffle period, how can the new brand break the three major problems?
- How to find hot projects in 2022? Dena community project progress follow-up, there is always a dish for you (1)
- 有个疑问 flink sql cdc 的话可以设置并行度么, 并行度大于1会有顺序问题吧?
- Pytest (5) - assertion
猜你喜欢
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Bert fine tuning skills experiment
openresty ngx_ Lua execution phase
qrcode:将文本生成二维码
端口,域名,协议。
Apache build web host
Open source SPL optimized report application coping endlessly
Introduce reflow & repaint, and how to optimize it?
Hmi-30- [motion mode] the module on the right side of the instrument starts to write
Character painting, I use characters to draw a Bing Dwen Dwen
随机推荐
GFS分布式文件系统
ELK日志分析系统
Azkaban实战
D3js notes
Simple use of devtools
腾讯云,实现图片上传
When to catch an exception and when to throw an exception- When to catch the Exception vs When to throw the Exceptions?
Bumblebee: build, deliver, and run ebpf programs smoothly like silk
tuple and point
Hmi-32- [motion mode] add light panel and basic information column
Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 1)
Port, domain name, protocol.
spoon插入更新oracle数据库,插了一部分提示报错Assertion botch: negative time
Introduce reflow & repaint, and how to optimize it?
返回二叉树中两个节点的最低公共祖先
The database and recharge are gone
2. Common request methods
Android advanced interview question record in 2022
Hmi-30- [motion mode] the module on the right side of the instrument starts to write
Leetcode takes out the least number of magic beans