当前位置:网站首页>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);
}
参考文章
注意
当只有右子树时,遍历顺序是根节点->右子树,其余情况都是左子树->右子树->根节点
边栏推荐
- 如何使用 Authing 单点登录,集成 Discourse 论坛?
- Dapr 与 NestJs ,实战编写一个 Pub & Sub 装饰器
- 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
- tensorflow2.0 handwritten digit recognition (tensorflow handwriting recognition)
- Data Mining-04
- 透过开发抽奖小程序,体会创新与迭代
- CloudCompare & PCL ICP registration (point to face)
- Istio投入生产的障碍以及如何解决这些问题
- 数据挖掘-04
- 芝加哥丰田技术学院 | Leveraging Natural Supervision for Language Representation Learning and Generation(利用自然监督进行语言表示学习和生成)
猜你喜欢

Multi-threaded cases - blocking queue

Process sibling data into tree data

批量替换Word中的表格为图片并保存

CloudCompare & PCL ICP registration (point to face)

10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享

树和二叉树的转换

34、树莓派进行人体姿态检测并进行语音播报

Efficiency tools to let programmers get off work earlier

Windows 安装PostgreSQL

如何使用 Authing 单点登录,集成 Discourse 论坛?
随机推荐
SAP ABAP OData 服务如何支持创建(Create)操作试读版
Why does the maximum plus one equal the minimum
模型运营是做什么的(概念模型数据库)
易周金融分析 | 银行ATM机智能化改造提速;互联网贷款新规带来挑战
Feign 从注册到调用原理分析
postgresql之page分配管理(一)
Js手写函数之new的模拟实现
VIP的实现原理
安全又省钱,“15岁”老小区用上管道燃气
What Can Service Mesh Learn from SDN?
LeetCode_动态规划_中等_377.组合总和 Ⅳ
34、树莓派进行人体姿态检测并进行语音播报
ECCV22|只能11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT
The basic knowledge of scripting language Lua summary
NebulaGraph v3.2.0 Performance Report
PyTorch 进阶之路:在 GPU 上训练深度神经网络
JMP Pro 16.0软件安装包下载及安装教程
态路小课堂丨浅谈优质光模块需要具备的条件!
Grafana9.0发布,Prometheus和Loki查询生成器、全新导航、热图面板等新功能!
如何降低Istio服务网格中Envoy的内存开销