当前位置:网站首页>PAT Class A 1130 Infix Expressions
PAT Class A 1130 Infix Expressions
2022-08-02 17:06:00 【keyboard sonata】
Given a syntactic binary tree, please output the corresponding infix expression, and use parentheses to reflect the precedence of operators.
Input format
The first line contains the integer N representing the number of summary points of the binary tree.Next N lines, each giving information about a node in the following format (line i corresponds to node i):
data left_child right_child
where data is a string with a length not exceeding 10, left_child and right_child are the left and right child node numbers of the node respectively.All nodes are numbered from 1 to N, NULL is represented by -1.
The following two figures correspond to Example 1 and Example 2, respectively.
11.JPG 222.JPG
Output format
Please output the infix expression on one line, and use parentheses to reflect the operator's precedence.Please note that there should be no extra parentheses and no spaces between any symbols.
Data Range
1≤N≤20
Input Example 1:
8
* 8 7
a -1 -1
* 4 1
+ 2 5
b -1 -1
d -1 -1
- -1 6
c -1 -1
Example output 1:
(a+b)*(c*(-d))
Example 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))
Solution 1:
#include using namespace std;const int N = 30;string a[N];int n, l[N], r[N], f[N];int root;void dfs(int u){if(u > n) return;if(u == -1) return;if(!((l[u] == -1 && r[u] == -1) || u == root)) cout << '(';dfs(l[u]);cout << a[u];dfs(r[u]);if(!((l[u] == -1 && r[u] == -1) || u == root)) cout << ')';}int main(){cin >> n;memset(l, -1, sizeof l);memset(r, -1, sizeof r);memset(f, -1, sizeof f);for(int i = 1; i <= n; i ++ ){cin >> a[i];cin >> l[i] >> r[i];if(l[i] != -1) f[l[i]] = i;if(r[i] != -1) f[r[i]] = i;}root = 1;while(f[root] != -1) root = f[root];dfs(root);return 0;} Solution 2:
#include using namespace std;const int N = 30;string a[N];int n, l[N], r[N], f[N];bool is_leaf[N];int root;string dfs(int u){string left, right;if(l[u] != -1){left = dfs(l[u]);if(!is_leaf[l[u]]) left = "(" + left + ")";}if(r[u] != -1){right = dfs(r[u]);if(!is_leaf[r[u]]) right = "(" + right + ")";}return left + a[u] + right;}int main(){cin >> n;memset(l, -1, sizeof l);memset(r, -1, sizeof r);memset(f, -1, sizeof f);for(int i = 1; i <= n; i ++ ){cin >> a[i];cin >> l[i] >> r[i];if(l[i] != -1) f[l[i]] = i;if(r[i] != -1) f[r[i]] = i;if(l[i] == -1 && r[i] == -1) is_leaf[i] = true;}root = 1;while(f[root] != -1) root = f[root];cout << dfs(root);return 0;} 边栏推荐
- 【无标题】
- js中的join()方法
- 加载事件的用法
- Redis最新6.27安装配置笔记及安装和常用命令快速上手复习指南
- Impulse response invariant method and bilinear transformation method for IIR filter design
- 两分钟录音就可秒变语言通!火山语音音色复刻技术如何修炼而成?
- 基于mobileNet实现狗的品种分类(迁移学习)
- Application software code signing certificate
- VsCode更新后,怎么使用使用快捷键同时生成多个元素
- codeforces Linova and Kingdom
猜你喜欢
随机推荐
中国服装行业已形成一套完整的产业体系
How to check the WeChat applet server domain name and modify it
延时函数-定时器
什么是hashCode?
Servlet 技术1
双亲委派机制
为什么四个字节的float表示的范围比八个字节的long要广
Application software code signing certificate
MySQL 自增主键
Redis的5中数据类型总结
Redis最新6.27安装配置笔记及安装和常用命令快速上手复习指南
为什么四个字节的float表示的范围比八个字节的long要广?
散列表简述
【Leetcode字符串--字符串变换/进制的转换】HJ1.字符串最后一个单词的长度 HJ2.计算某字符出现次数 HJ30.字符串合并处理
mysql 自动添加创建时间、更新时间
PAT甲级 1145 哈希 - 平均查找时间
lambda表达式、Stream接口及Optional类
MySQL 高级(进阶) SQL 语句 (一)
SOA(面向服务架构)是什么?
机械键盘失灵









