当前位置:网站首页>PAT 1162 Postfix Expression(25)
PAT 1162 Postfix Expression(25)
2022-08-01 12:51:00 【此杭非彼航】
题目
AC代码
#include<bits/stdc++.h>
using namespace std;
struct Node{
string data;
int left,right;
};
unordered_map<int,Node> tree;
string post_order(int root){
Node tmp=tree[root];
if(tmp.left==-1&&tmp.right==-1){
return "("+tmp.data+")";
}
else if(tmp.left==-1){
return "("+tmp.data+post_order(tmp.right)+")";
}
else{
return "("+post_order(tmp.left)+post_order(tmp.right)+tmp.data+")";
}
}
int main()
{
int n;
cin>>n;
int vis[n+1],root=0;
fill(vis,vis+n+1,0);
for(int i=1;i<=n;i++){
string x;
int y,z;
cin>>x>>y>>z;
tree[i]={
x,y,z};
vis[y]=vis[z]=1;
}
for(int i=1;i<=n;i++){
if(vis[i]==0){
root=i;
break;
}
}
cout<<post_order(root);
}
参考文章
注意
当只有右子树时,遍历顺序是根节点->右子树,其余情况都是左子树->右子树->根节点
边栏推荐
- 力扣160题,相交链表
- 10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享
- 脚本语言Lua的基础知识总结
- 【无标题】
- Grafana 9.0 released, Prometheus and Loki query builders, new navigation, heatmap panels and more!
- uniapp读取和写入文件
- SQL函数 %SQLUPPER
- Istio Pilot代码深度解析
- Beyond Compare 4 trial period expires
- 【StoneDB Class】Introduction Lesson 2: Analysis of the Overall Architecture of StoneDB
猜你喜欢
PanGu-Coder:函数级的代码生成模型
HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
postgresql之page分配管理(一)
Based on 10 years of experience in stability assurance, what are the three key questions to be answered in failure recovery?|TakinTalks big coffee sharing
那些利用假期学习的职场人,后来都怎么样了?
Beyond Compare 4 trial period expires
找出相同属性值的对象 累加数量 汇总
PanGu-Coder:函数级的代码生成模型
NebulaGraph v3.2.0 Performance Report
消息中间件解析 | 如何正确理解软件应用系统中关于系统通信的那些事?
随机推荐
模型运营是做什么的(概念模型数据库)
Programmer's Romantic Tanabata
通讯录(静态版)(C语言)(VS)
实现集中式身份认证管理的案例
芝加哥丰田技术学院 | Leveraging Natural Supervision for Language Representation Learning and Generation(利用自然监督进行语言表示学习和生成)
态路小课堂丨浅谈优质光模块需要具备的条件!
【StoneDB Class】入门第二课:StoneDB 整体架构解析
CCS软件安装教程(超级详细)「建议收藏」
How to integrate 3rd party service center registration into Istio?
关于Request复用的那点破事儿。研究明白了,给你汇报一下。
Feign 从注册到调用原理分析
全链路灰度在数据库上我们是怎么做的?
formatdatetime函数 mysql(date sub函数)
[Unity3D Plugin] AVPro Video Plugin Share "Video Player Plugin"
批量替换Word中的表格为图片并保存
观察者模式
The obstacles to put Istio into production and how we solve them
四足机器人软件架构现状分析
Find objects with the same property value Cumulative number Summarize
找出相同属性值的对象 累加数量 汇总