当前位置:网站首页>Suffix expression (summer vacation daily question 4)
Suffix expression (summer vacation daily question 4)
2022-07-23 15:49:00 【sweetheart7-7】
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 N N, Represents the number of nodes . Node number 1 ∼ N 1∼N 1∼N.
Next N N N That's ok , Each row gives information about a node ( The first i i i Row corresponds to the second row i i i Nodes ), The format is :
data left_child right_child
among ,data It's not more than 10 10 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 1≤N≤20 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)%))+)
#include<iostream>
#include<cstring>
using namespace std;
const int N = 30;
int n;
string a[N];
int l[N], r[N];
bool st[N];
void dfs(int u){
cout << '(';
if(l[u] == -1 && r[u] != -1){
cout << a[u];
dfs(r[u]);
}else{
if(l[u] != -1) dfs(l[u]);
if(r[u] != -1) dfs(r[u]);
cout << a[u];
}
cout << ')';
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i] >> l[i] >> r[i];
if(l[i] != -1) st[l[i]] = true;
if(r[i] != -1) st[r[i]] = true;
}
int head = -1;
for(int i = 1; i <= n; i++)
if(!st[i]) head = i;
dfs(head);
return 0;
}
边栏推荐
猜你喜欢

Find a specific number in an ordered array (binary search or half search)
![[7.16] code source - [array division] [disassembly] [select 2] [maximum common divisor]](/img/fd/ffddb3ac35e946215a0582f09f278a.png)
[7.16] code source - [array division] [disassembly] [select 2] [maximum common divisor]

7.13web safety operation

Deep understanding of L1 and L2 regularization

适用于顺序磁盘访问的1分钟法则

C语言经典例题-逆序打印输入的两位数

Clickhouse, let the query fly!!!

800V高压快充落地进程加快均胜电子获5亿欧元项目定点
![[ctfhub] the data of JWT header and payload are transmitted in clear text. If sensitive information is contained in it, sensitive information will be leaked. Try to find the flag. Format is flag{}](/img/d0/133d628a304f5c6b5f0d514c9fe222.jpg)
[ctfhub] the data of JWT header and payload are transmitted in clear text. If sensitive information is contained in it, sensitive information will be leaked. Try to find the flag. Format is flag{}

【攻防世界WEB】难度三星9分入门题(中):ics-05、easytornado
随机推荐
地图附近名片流量主小程序开发
uniapp路由跳转的六种方式
pydensecrf安装
STL deque
One minute rule for sequential disk access
Open source quadruped robot with design drawings and code "suggestions collection"
Chapter 4 event handling of quick mastering QML
[200 opencv routines] 225. Fourier descriptor for feature extraction
对C语言最基本的代码解释
Modify SSH command line[ [email protected] ]Color
《快速掌握QML》第四章 事件处理
C语言经典例题-逆序打印输入的两位数
select......for update 语句的功能是什么? 会锁表还是锁行?
[pyGame actual combat] aircraft shooting masterpiece: fierce battle in the universe is imminent... This super classic shooting game should also be taken out and restarted~
种种迹象表明,Apple将有望支持AV1
[7.16] code source - [array division] [disassembly] [select 2] [maximum common divisor]
bug修改
Safe and reasonable use of electricity to harvest a cool "summer"
第三篇 RBAC权限管理 数据库设计详解
day1