当前位置:网站首页>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;
}
边栏推荐
- The difference between interceptor and filter
- 性能测试如何创造业务价值
- 9D电影是怎样的?(+维度空间常识)
- R language dplyr package filter function filters dataframe data. If the name of the data column (variable) to be filtered contains quotation marks, you need to use!! SYM syntax processing, otherwise n
- C file input operation
- 守望先锋世界观架构 ——(一款好的游戏是怎么来的)
- [paper reading] Ca net: leveraging contextual features for lung cancer prediction
- Introduction of Ethernet PHY layer chip lan8720a
- 【测试开发】一文带你了解什么是软件测试
- 教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
猜你喜欢

Thread application instance

Learning summary of MySQL advanced 6: concept and understanding of index, detailed explanation of b+ tree generation process, comparison between MyISAM and InnoDB

PyTorch函数中的__call__和forward函数

仿京东放大镜效果(pink老师版)

Quanzhi A33 uses mainline u-boot

论文导读 | 机器学习在数据库基数估计中的应用

数据降维——因子分析

为什么要做企业固定资产管理系统,企业如何加强固定资产管理

Juypter notebook modify the default open folder and default browser

Data dimensionality reduction principal component analysis
随机推荐
云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
Have you stepped on the nine common pits in the e-commerce system?
从list转化成map的时候,如果根据某一属性可能会导致key重复而异常,可以设置处理这种重复的方式
How performance testing creates business value
Fastdfs installation
Thread application instance
新手必看,點擊兩個按鈕切換至不同的內容
#gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
Yolov3 trains its own data set to generate train txt
Page title component
Juypter notebook modify the default open folder and default browser
Reduce -- traverse element calculation. The specific calculation formula needs to be passed in and combined with BigDecimal
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
QT中的QPropertyAnimation使用和toast案列
End to end object detection with transformers (Detr) paper reading and understanding
线程应用实例
Preprocessing and preprocessing macros
Machine learning notes - time series prediction research: monthly sales of French champagne
性能测试如何创造业务价值
R language dplyr package filter function filters dataframe data. If the name of the data column (variable) to be filtered contains quotation marks, you need to use!! SYM syntax processing, otherwise n