当前位置:网站首页>4274. 后缀表达式
4274. 后缀表达式
2022-06-24 07:07:00 【Ray.C.L】

思路:建树,后序遍历,对负号特判
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 30;
string v[N];
int st[N],l[N],r[N];
void dfs(int u){
cout << "(" ;
if(l[u] != -1 && r[u] != -1){
dfs(l[u]);
dfs(r[u]);
cout << v[u] ;
}else if(l[u] == -1 && r[u] == -1){
cout << v[u] ;
}else{
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;
}
int root;
for(int i = 1; i <= n; i ++)
if(!st[i])
root = i;
dfs(root);
return 0;
}
边栏推荐
- Smart power plant: how to make use of easycvr to build a safe, stable, green and environment-friendly intelligent inspection platform
- K8s deployment of highly available PostgreSQL Cluster -- the road to building a dream
- 数据中台:数据采集和抽取的技术栈详解
- rpiplay实现树莓派AirPlay投屏器
- orb slam build bug: undefined reference to symbol ‘_ ZN5boost6system15system_ categoryEv‘
- 基于单片机开发的酒精浓度测试仪方案
- 【量化投资】离散傅里叶变换求数组周期
- MyCAT读写分离与MySQL主从同步
- 提高INSERT速度
- Deep learning and neural networks: the six most noteworthy trends
猜你喜欢

Detailed explanation of Base64 coding and its variants (to solve the problem that the plus sign changes into a space in the URL)

用VNC Viewer的方式远程连接无需显示屏的树莓派

【牛客】HJ1 字符串最后一个单词的长度

input的聚焦后的边框问题

数据中台:数据中台技术架构详解

2022.06.23(LC_144,94,145_二叉树的前序、中序、后序遍历)

阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍

110. 平衡二叉树-递归法

“不平凡的代理初始值设定不受支持”,出现的原因及解决方法

MySQL | 视图《康师傅MySQL从入门到高级》笔记
随机推荐
数组相向指针系列
216. 组合总和 III-枚举法
利用sonar做代码检查
Analyze the meaning of Internet advertising terms CPM, CPC, CPA, CPS, CPL and CPR
Rsync for file backup
every()、map()、forEarch()方法。数组里面有对象的情况
rsync做文件备份
Picture tools
数云发布2022美妆行业全域消费者数字化经营白皮书:全域增长破解营销难题
Determination of monocular and binocular 3D coordinates
Floating error waiting for changelog lock
[pytoch basic tutorial 31] youtubednn model analysis
何时使用RDD和DataFrame/DataSet
The form image uploaded in chorme cannot view the binary image information of the request body
一文详解|增长那些事儿
数据库迁移从PostgreSQL迁移到 MYSQL
QT source code analysis -- QObject (2)
表单图片上传在Chorme中无法查看请求体的二进制图片信息
rpiplay实现树莓派AirPlay投屏器
小程序wx.show