当前位置:网站首页>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);
}
参考文章
注意
当只有右子树时,遍历顺序是根节点->右子树,其余情况都是左子树->右子树->根节点
边栏推荐
猜你喜欢
随机推荐
How does the SAP ABAP OData service support the Create operation trial version
Batch replace tables in Word with pictures and save
CCS软件安装教程(超级详细)「建议收藏」
VIP的实现原理
Detailed explanation of table join
高仿项目协作工具【Worktile】,从零带你一步步实现组织架构、网盘、消息、项目、审批等功能
SQL function SQRT
formatdatetime函数 mysql(date sub函数)
SAP ABAP OData 服务如何支持创建(Create)操作试读版
SQL函数 %SQLUPPER
CloudCompare & PCL ICP registration (point to face)
[Cloud Enjoying Freshness] Community Weekly Vol.73- DTSE Tech Talk: 1 hour in-depth interpretation of SaaS application system design
并发编程10大坑,你踩过几个?
Dapr 与 NestJs ,实战编写一个 Pub & Sub 装饰器
Windows 安装PostgreSQL
数字孪生北京故宫,元宇宙推进旅游业进程
全链路灰度在数据库上我们是怎么做的?
数据挖掘-04
树和二叉树的转换
Data Mining-04