当前位置:网站首页>由中序遍历和后序遍历得到前序遍历(树的遍历)
由中序遍历和后序遍历得到前序遍历(树的遍历)
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);
}
边栏推荐
- oracle inner join and outer join
- @Accessors 注解详解
- C语言 void和void *(无类型指针)
- ImportError: libGL.so.1: cannot open shared object file: No such file or directory
- kettle 安装与配置
- 每天填坑,精卫填坑第二集,TX1 配置从固态启动,安装Pytorch
- Week 7 Review
- 磷脂-聚乙二醇-靶向新生血管靶向肽APRPG,DSPE-PEG-APRPG
- @Autowired详解[email protected]在static属性上的使用
- SSM integration
猜你喜欢

啃瓜记录又一天
[email protected]在static属性上的使用"/>@Autowired详解[email protected]在static属性上的使用

Source Insight 使用教程(2)——常用功能

String comparison size in MySQL (date string comparison problem)

语义分割标签即像素值的巨坑,transforms.ToTensor()的错误使用

PCL—点云数据分割

活体检测 Adaptive Normalized Representation Learning for GeneralizableFace Anti-Spoofing 阅读笔记

FreeRTOS内核详解(1) —— 临界段保护原理

kettle 安装与配置

DSPE-PEG-DBCO Phospholipid-Polyethylene Glycol-Dibenzocyclooctyne A Linear Heterobifunctional Pegylation Reagent
随机推荐
科研试剂DMPE-PEG-Mal 二肉豆蔻酰磷脂酰乙醇胺-聚乙二醇-马来酰亚胺
js 数组去重的常用方法
骨架效果 之高级渐变,适用图片等待时
STM32 CAN过滤器
【程序人生】做了多年的运维,靠什么转行拿下12K+年终奖的薪资?
sh: 1: curl: not found
PCL—点云数据分割
【我的创作纪念日】 3周年
subprocess.CalledProcessError: Command ‘pip install ‘thop‘‘ returned non-zero exit status 1.
广州华为面试总结
MySQL分区表详解
@Autowired与@Resource区别
@ApiModel 和 @ApiModelProperty
Cut out web icons through PS 2021
mysql卸载详细教程
Deveco studio 鸿蒙app访问网络详细过程(js)
npm和package.json
basic operator
客户评分控件
The usage of json type field in mysql