当前位置:网站首页>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);
}
参考文章
注意
当只有右子树时,遍历顺序是根节点->右子树,其余情况都是左子树->右子树->根节点
边栏推荐
- iframe tag attribute description detailed [easy to understand]
- Batch replace tables in Word with pictures and save
- NebulaGraph v3.2.0 性能报告
- Js手写函数之new的模拟实现
- CloudCompare & PCL ICP registration (point to face)
- Multi-threaded cases - blocking queue
- 数据湖 delta lake和spark版本对应关系
- 观察者模式
- 阿里云官方 Redis 开发规范
- shell 中的 分发系统 expect脚本 (传递参数、自动同步文件、指定host和要传输的文件、(构建文件分发系统)(命令批量执行))
猜你喜欢

树和二叉树的转换

ECCV22|只能11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT

HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界

快速幂---学习笔记

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

Feign 从注册到调用原理分析

论文笔记All about Eve: Execute-Verify Replication for Multi-Core Servers

Towhee 每周模型

【2022蓝帽杯】file_session && 浅入opcode

Beyond Compare 4 trial period expires
随机推荐
tensorflow2.0手写数字识别(tensorflow手写体识别)
This article will take you to thoroughly clarify the working mechanism of certificates in Isito
软件测试之发现和解决bug
那些利用假期学习的职场人,后来都怎么样了?
CloudCompare&PCL ICP配准(点到面)
8. How does the SAP ABAP OData service support the Create operation
shell 中的 分发系统 expect脚本 (传递参数、自动同步文件、指定host和要传输的文件、(构建文件分发系统)(命令批量执行))
uniapp读取和写入文件
让程序员早点下班的效率工具
JMP Pro 16.0软件安装包下载及安装教程
VIP的实现原理
实现集中式身份认证管理的案例
Grafana 9.0 released, Prometheus and Loki query builders, new navigation, heatmap panels and more!
全链路灰度在数据库上我们是怎么做的?
leetcode:1201. 丑数 III【二分 + 数学 + 容斥原理】
CCS软件安装教程(超级详细)「建议收藏」
蔚来又一新品牌披露:产品价格低于20万
windows IDEA + PHP+xdebug 断点调试
fh511小风扇主控芯片 便携式小风扇专用8脚IC 三档小风扇升压芯片sop8
将同级数据处理成树形数据