当前位置:网站首页>4274. 后缀表达式-二叉表达式树
4274. 后缀表达式-二叉表达式树
2022-07-02 18:19:00 【linengcs】
- 二叉表达式树的后序遍历就是后缀表达式
- 特例:怎么区分表达式中的正号与加号,负号和减号呢?
答:如果某个节点是“+”或“-”号,并且其左子树为空时,则该符号为正(负)号,而不是加减号,那么对该节点进行先序遍历而不是后序遍历
ACCode
#include <bits/stdc++.h>
using namespace std;
const int N = 25;
int t[N][2], vis[N];
int n;
string v[N];
void traverse(int node)
{
if (node == -1)
return;
if ((v[node] == "-" || v[node] == "+") && t[node][0] == -1)
{
cout << "(" << v[node];
traverse(t[node][1]);
cout << ")";
}
else
{
cout << "(";
traverse(t[node][0]);
traverse(t[node][1]);
cout << v[node];
cout << ")";
}
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> v[i] >> t[i][0] >> t[i][1];
if (t[i][0] != -1)
vis[t[i][0]] = 1;
if (t[i][1] != -1)
vis[t[i][1]] = 1;
}
int p;
for (int i = 1; i <= n; i++)
{
if (!vis[i])
{
p = i;
break;
}
}
traverse(p);
return 0;
}
边栏推荐
- 以太网PHY层芯片LAN8720A简介
- Masa framework - DDD design (1)
- 教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
- 守望先锋世界观架构 ——(一款好的游戏是怎么来的)
- Codeworks 5 questions per day (1700 average) - day 4
- Microservice technology - distributed global ID in high concurrency
- Date tool class (updated from time to time)
- Machine learning notes - time series prediction research: monthly sales of French champagne
- MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree
- The R language dplyr package rowwise function and mutate function calculate the maximum value of multiple data columns in each row in the dataframe data, and generate the data column (row maximum) cor
猜你喜欢
思维意识转变是施工企业数字化转型成败的关键
How can retail enterprises open the second growth curve under the full link digital transformation
End to end object detection with transformers (Detr) paper reading and understanding
Markdown basic grammar
消息队列消息丢失和消息重复发送的处理策略
Excel finds the same value in a column, deletes the row or replaces it with a blank value
yolov3 训练自己的数据集之生成train.txt
Talk about the design of red envelope activities in e-commerce system
论文导读 | 机器学习在数据库基数估计中的应用
What is 9D movie like? (+ common sense of dimension space)
随机推荐
Codeworks 5 questions per day (1700 average) - day 4
Yunna | why use the fixed asset management system and how to enable it
以太网PHY层芯片LAN8720A简介
The R language dplyr package rowwise function and mutate function calculate the maximum value of multiple data columns in each row in the dataframe data, and generate the data column (row maximum) cor
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
Npoi export Excel2007
Which video recording software is better for the computer
Golang:[]byte to string
Learn the knowledge points of eight part essay ~ ~ 1
codeforces每日5题(均1700)-第四天
Horizontal ultra vires and vertical ultra vires [easy to understand]
Yolov3 trains its own data set to generate train txt
Qpropertyanimation use and toast case list in QT
线程应用实例
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
[pytorch learning notes] tensor
2022软件工程期末考试 回忆版
The difference between interceptor and filter
Page title component