当前位置:网站首页>4274. Suffix expression
4274. Suffix expression
2022-07-27 08:37:00 【Pursue people far away】
Given a binary expression tree , Please output the corresponding suffix expression , Brackets are required to reflect the priority of the operator .
Input format
The first line contains integers N, Represents the number of nodes . Node number 1∼N.
Next N That's ok , Each row gives information about a node ( The first i Row corresponds to the second row i Nodes ), The format is :
data left_child right_child
among ,data It's not more than 10 Character string ,left_child and right_child Are the numbers of the left and right child nodes of the node .
No child node ( namely NULL), Then use −1 Express .
The following two figures correspond to the two examples given .


Output format
Output answers in one line , There must be no spaces between expression symbols .
Data range
1≤N≤20
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)%))+)
Code :
// Tree suffix expression
#include <bits/stdc++.h>
using namespace std;
const int N = 25;
string v[N];
int l[N], r[N];
bool st[N];
int root;
void dfs(int u)
{
cout << '(';
if (l[u] != -1 && r[u] != -1) // Currently operator
{
dfs(l[u]);
dfs(r[u]);
cout << v[u];
}
else if (l[u] == -1 && r[u] == -1) // Current value
{
cout << v[u];
}
else // Only the right subtree Currently negative
{
cout << v[u];
dfs(r[u]);
}
cout << ')';
}
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> v[i] >> l[i] >> r[i];
if (l[i] != -1)
st[l[i]] = 1;
if (r[i] != -1)
st[r[i]] = 1;
}
for (int i = 1; i <= n; i++)
if (!st[i])
root = i;
dfs(root);
return 0;
}
边栏推荐
- 缓存一致性与内存屏障
- Introduction, installation and use of netdata performance monitoring tool
- All in one 1319 - queue for water
- 无法获取下列许可SOLIDWORKS Standard,无法找到使用许可文件。(-1,359,2)。
- Solution of database migration error
- [ciscn2019 southeast China division]web11 1
- [netding cup 2020 rosefinch group]nmap 1 two solutions
- Breadth first search
- 【渗透测试工具分享】【dnslog服务器搭建指导】
- [MRCTF2020]PYWebsite 1
猜你喜欢

VS Code中#include报错(新建的头文件)

第2章 前台数据展现

All in one 1251 - Fairy Island for medicine (breadth first search)

Flask project configuration

MCDF顶层验证方案

Bandwidth and currency

Element display mode: block level, inline, inline block, nesting specification, display mode conversion

Solution of database migration error

Day3 -- flag state holding, exception handling and request hook

Flutter 渲染机制——GPU线程渲染
随机推荐
第2章 前台数据展现
Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering
帮忙发几个招聘,有兴趣可以看看
Connection failed during installation of ros2 [ip: 91.189.91.39 80]
Cache consistency and memory barrier
如何在qsim查看软件对象的实例?
I drew a Gu ailing with characters!
How to permanently set source
Flask project configuration
Background image related applications - full, adaptive
03.使用引号来监听对象嵌套值的变化
海量数据肖枫:共建共治openGauss根社区,共享欣欣向荣新生态
SSTI template injection
All in one 1329 cells (breadth first search)
Pass parameters and returned responses of flask
海关总署:这类产品暂停进口
Openresty + keepalived 实现负载均衡 + IPV6 验证
Set set
MCDF顶层验证方案
Realization of specification management and specification option management functions