当前位置:网站首页>4274. 后缀表达式
4274. 后缀表达式
2022-06-22 00:04:00 【NEFU AB-IN】
Powered by:NEFU AB-IN
4274. 后缀表达式
题意
第一行包含整数 N,表示节点数量。节点编号 1∼N。
接下来 N 行,每行给出一个节点的信息(第 i 行对应第 i 个节点),格式为:
data left_child right_child
其中,data 是一个不超过 10 个字符的字符串,left_child 和 right_child 分别是该节点的左右子节点的编号。
没有子节点(即 NULL),则用 −1 表示。思路
结论:表达式树的x序遍历 等价于 x缀表达式
后缀表达式:
- 若是二元运算符,则正常后序遍历
- 注意负号,则为先序遍历 (特征:当一个节点为
-,且左边没有儿子)
代码
/* * @Author: NEFU AB-IN * @Date: 2022-06-21 09:49:36 * @FilePath: \ACM\Acwing\4274\4274.cpp * @LastEditTime: 2022-06-21 10:24:53 */ #include <bits/stdc++.h> using namespace std; #define int long long #define SZ(X) ((int)(X).size()) #define IOS \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define DEBUG(X) cout << #X << ": " << X << endl; typedef pair<int, int> PII; const int INF = INT_MAX; const int N = 1e6 + 10; signed main() { IOS; int n; cin >> n; vector<int> ls(n + 1), rs(n + 1); vector<string> e(n + 1); vector<int> deg(n + 1); // 入度 for (int i = 1; i <= n; ++i) { string s; int l, r; cin >> s >> l >> r; if (l != -1) ls[i] = l, deg[l]++; if (r != -1) rs[i] = r, deg[r]++; e[i] = s; } int root = 1; for (int i = 1; i <= n; ++i) { if (!deg[i]) { root = i; break; } } function<void(int)> dfs = [&](int u) { cout << "("; if (ls[u] && rs[u]) { dfs(ls[u]); dfs(rs[u]); cout << e[u]; } else if (!ls[u] && !rs[u]) { cout << e[u]; } else { cout << e[u]; dfs(rs[u]); } cout << ")"; }; dfs(root); return 0; }
边栏推荐
- Graphical understanding of the article "text classification of Sina News Based on tensorflow+rnn"
- The next goal of digital transformation: providing just in time information
- Opérations de bits bits et
- View local IP address in vscode
- Broadening - simple strategy test
- Summary of new MySQL 8.0 features
- Pytorch learning 09: basic matrix operations
- eVC4编的程序不能在emulator上运行
- [environment stepping on the pit] open the picture with OpenCV and report an error
- Pytorch learning 08: splicing and splitting
猜你喜欢

Tom Ellison, the new CFO of mendix, promoted the next stage of rapid growth of the company through the transformation of the leadership team

鸿蒙OS学习(轮播图、列表、图标)

前加后加探索和函数调用探索

Tensorflow环境搭建

The importance of rational selection of seal clearance of hydraulic slip ring

2. 两数相加

花了2小时,搭建了一个物联网项目,值了 ~

RNN的简单整理

企业可通过4个方法提高数据库安全

位运算位与
随机推荐
Summary of new MySQL 8.0 features
0x00007ffff3d3ecd0 in _IO_vfprintf_internal (s=0x7ffff40b5620 <_IO_2_1_stdout_>
位运算位与
Mendix公司新任CFO Tom Ellison通过领导团队转型推动公司下一阶段高速增长
The interviewer asked me how the order ID was generated? Is it not MySQL self incrementing primary key?
Pytorch learning 07:broadcast broadcast - automatic extension
[examination skills] memory method and simple derivation of Green formula
Pytorch learning 03: tensor data types and some operations
Version dynamic | exchangis 1.0.0-rc1 version release
0x00007ffff3d3ecd0 in _IO_vfprintf_internal (s=0x7ffff40b5620 <_IO_2_1_stdout_>
What are the current mainstream overseas social media? Could you give me a brief introduction?
力扣每日一题-第24天-485.最大连续1的个数
12 initialization of beautifulsoup class
lvgl使用demo示例及说明1
Emperor Taizong of Tang Dynasty played the "heartbeat mechanism" of microservices to the extreme!
Acwing match 56 Weekly
活动窗口(active),焦点窗口(focused),前台窗口(foreground)的区别
聚宽 - 简单策略试验
高德地图--根据地理位置获取经纬度
跨境贸易和跨境电商的三大区别简单分析