当前位置:网站首页>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);
}
参考文章
注意
当只有右子树时,遍历顺序是根节点->右子树,其余情况都是左子树->右子树->根节点
边栏推荐
- 拥抱NFV,Istio 1.1 将支持多网络平面
- 8. How does the SAP ABAP OData service support the Create operation
- 多线程案例——阻塞式队列
- Batch replace tables in Word with pictures and save
- 意大利普拉托华社将游行示威 盼解决安全问题
- shell 中的 分发系统 expect脚本 (传递参数、自动同步文件、指定host和要传输的文件、(构建文件分发系统)(命令批量执行))
- This article will take you to thoroughly clarify the working mechanism of certificates in Isito
- 故障007:dexp导数莫名中断
- 树和二叉树的转换
- How to Integrate Your Service Registry with Istio?
猜你喜欢
随机推荐
蔚来又一新品牌披露:产品价格低于20万
嵌入式开发:创建和使用可移植类型的7个技巧
CloudCompare & PCL ICP registration (point to face)
LeetCode_动态规划_中等_313.超级丑数
uniapp读取和写入文件
消息中间件解析 | 如何正确理解软件应用系统中关于系统通信的那些事?
tensorflow2.0 handwritten digit recognition (tensorflow handwriting recognition)
拥抱NFV,Istio 1.1 将支持多网络平面
SQL functions STR
Detailed explanation of table join
Software designer test center summary (interior designer personal summary)
Find objects with the same property value Cumulative number Summarize
50W+小程序开发者背后的数据库降本增效实践
JMP Pro 16.0 software installation package download and installation tutorial
数据挖掘-03
CloudCompare&PCL ICP配准(点到面)
并发编程10大坑,你踩过几个?
四足机器人软件架构现状分析
程序员的自我修养
浏览器存储








