当前位置:网站首页>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;
}
边栏推荐
- 2022 compilation principle final examination recall Edition
- codeforces每日5题(均1700)-第四天
- Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
- 2022.7.1-----leetcode. two hundred and forty-one
- golang:[]byte转string
- 潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
- MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format
- Digital scroll strip animation
- 聊聊电商系统中红包活动设计
- SIFT特征点提取「建议收藏」
猜你喜欢

Develop fixed asset management system, what voice is used to develop fixed asset management system

Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent

开发固定资产管理系统,开发固定资产管理系统用什么语音
![[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter](/img/c4/3bba96fda92328704c2ddd929dcdf6.png)
[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter

MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree

聊聊电商系统中红包活动设计

Usage of ieda refactor

Excel查找一列中的相同值,删除该行或替换为空值

Yolov3 trains its own data set to generate train txt

新手必看,點擊兩個按鈕切換至不同的內容
随机推荐
横向越权与纵向越权[通俗易懂]
Page title component
In pytorch function__ call__ And forward functions
使用xml文件打印mybaties-log插件的方式
Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
C文件输入操作
[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
Emmet基础语法
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
Progress progress bar
SIFT feature point extraction "suggestions collection"
How performance testing creates business value
MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
Virtual machine initialization script, virtual machine mutual secret key free
全志A33使用主线U-Boot
Data dimensionality reduction principal component analysis
守望先锋世界观架构 ——(一款好的游戏是怎么来的)