当前位置:网站首页>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;
}
边栏推荐
- Stm32g0 USB DFU upgrade verification error -2
- Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
- C file input operation
- golang:[]byte转string
- Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
- 横向越权与纵向越权[通俗易懂]
- Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
- 【ERP软件】ERP体系二次开发有哪些危险?
- Excel finds the same value in a column, deletes the row or replaces it with a blank value
- 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
猜你喜欢

Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5

高频面试题

Machine learning notes - time series prediction research: monthly sales of French champagne

MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format

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

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

Yunna | why use the fixed asset management system and how to enable it

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

Stm32g0 USB DFU upgrade verification error -2

Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
随机推荐
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
What is 9D movie like? (+ common sense of dimension space)
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
Introduction of Ethernet PHY layer chip lan8720a
Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
2022 compilation principle final examination recall Edition
Emmet basic syntax
为什么要做企业固定资产管理系统,企业如何加强固定资产管理
Machine learning notes - time series prediction research: monthly sales of French champagne
C file input operation
MySQL高级(进阶)SQL语句
Date tool class (updated from time to time)
High frequency interview questions
Data dimensionality reduction principal component analysis
PHP parser badminton reservation applet development requires online system
高频面试题
MySQL advanced (Advanced) SQL statement
SIFT特征点提取「建议收藏」
FastDFS安装
性能测试如何创造业务价值