当前位置:网站首页>由中序遍历和后序遍历得到前序遍历(树的遍历)
由中序遍历和后序遍历得到前序遍历(树的遍历)
2022-08-02 03:22:00 【寒江飞冰】
给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。
输入格式:
输入第一行给出一个正整数N(≤30),是二叉树中结点的个数。第二行给出其后序遍历序列。第三行给出其中序遍历序列。数字间以空格分隔。
输出格式:
在一行中输出该树的前序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。
输入样例:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
输出样例:
4 1 3 2 6 5 7
#include<iostream>
using namespace std;
int a[31];
int b[31];
typedef struct Node{
int data;
Node *lchild;
Node *rchild;
}Node,*Tree;
Tree build(int *x,int *y,int n)
{
if(n<=0)
{
return NULL;
}
int *p=x;
while(p)
{
if(*p==*(y+n-1)) break;
p++;
}
Tree T = new Node;
T->data=*p;
int len=p-x;
T->lchild=build(x,y,len);
T->rchild=build(p+1,y+len,n-len-1);
return T;
}
void Print(Tree T)
{
if(T)
{
cout<<' '<<T->data;
Print(T->lchild);
Print(T->rchild);
}
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<n;i++) cin>>b[i];
Tree T;
T=build(b,a,n);
Print(T);
}
边栏推荐
- np.isnan()
- [Learning Records of Boxue Valley] Super summary, share with heart | Software Testing Interface Testing Basics
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/yolov5-5.0/models/commo
- AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'
- MySQL中JOIN的用法
- mysql卸载详细教程
- String comparison size in MySQL (date string comparison problem)
- 客户评分控件
- oracle内连接和外连接
- C语言 void和void *(无类型指针)
猜你喜欢
啃瓜记录第一天
科研试剂DMPE-PEG-Mal 二肉豆蔻酰磷脂酰乙醇胺-聚乙二醇-马来酰亚胺
配置mmdet来训练Swin-Transformer之一配置环境
小程序 van-cell 换行能左对齐问题
DSPE-PEG-Silane,DSPE-PEG-SIL,磷脂-聚乙二醇-硅烷修饰活性基团
canvas--饼状图
DSPE-PEG-DBCO Phospholipid-Polyethylene Glycol-Dibenzocyclooctyne A Linear Heterobifunctional Pegylation Reagent
Cut out web icons through PS 2021
UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the index ing argu
AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'
随机推荐
meime模块
[Basic Tutorial of Remote Control Development 1] Crazy Shell Open Source Formation Drone-GPIO (Remote Control Indicator Light Control)
Phospholipid-Polyethylene Glycol-Aldehyde DSPE-PEG-Aldehyde DSPE-PEG-CHO MW: 5000
一个结构体 = 另一个结构体(同类型结构体之间可直接赋值操作)
DSPE-PEG-DBCO Phospholipid-Polyethylene Glycol-Dibenzocyclooctyne A Linear Heterobifunctional Pegylation Reagent
[Learning Records of Boxue Valley] Super summary, share with heart | Software Testing Interface Testing Basics
sh: 1: curl: not found
docker 安装 sqlserver中的坑点
mysql中json类型字段用法
display,visibility,opacity
磷脂-聚乙二醇-靶向新生血管靶向肽APRPG,DSPE-PEG-APRPG
Phospholipid-polyethylene glycol-thiol, DSPE-PEG-Thiol, DSPE-PEG-SH, MW: 5000
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
SSM整合
Problems when yolov5 calls ip camera
Cloud server installation and deployment of Nacos 2.0.4 version
STM32 map文件解析
@Autowired注解的使用
Debian 10 NTP 服务配置
MySQL删除表数据 MySQL清空表命令 3种方法