当前位置:网站首页>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;
}
边栏推荐
- Talk about the design of red envelope activities in e-commerce system
- mysql备份后缀是什么_mysql备份还原
- [test development] takes you to know what software testing is
- How can retail enterprises open the second growth curve under the full link digital transformation
- MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format
- How to print mybats log plug-in using XML file
- Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
- 新手必看,點擊兩個按鈕切換至不同的內容
- Usage of ieda refactor
- 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
猜你喜欢

Novice must see, click two buttons to switch to different content

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

Juypter notebook modify the default open folder and default browser

Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management

性能测试如何创造业务价值

开发固定资产管理系统,开发固定资产管理系统用什么语音

使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星

xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机

使用CLion编译OGLPG-9th-Edition源码

End to end object detection with transformers (Detr) paper reading and understanding
随机推荐
云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
【测试开发】软件测试—概念篇
mysql备份后缀是什么_mysql备份还原
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
ICDE 2023|TKDE Poster Session(CFP)
Yunna | why use the fixed asset management system and how to enable it
Emmet基础语法
Markdown basic grammar
高频面试题
PHP asymmetric encryption method private key and public key encryption and decryption method
Compile oglpg-9th-edition source code with clion
Machine learning notes - time series prediction research: monthly sales of French champagne
PHP非对称加密方法私钥及公钥加密解密的方法
Advanced performance test series "24. Execute SQL script through JDBC"
Yolov3 trains its own data set to generate train txt
MySQL高级(进阶)SQL语句
GMapping代码解析[通俗易懂]
Page title component
"Patient's family, please come here" reading notes
潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失