当前位置:网站首页>由中序遍历和前序遍历得到后序遍历(树的遍历)
由中序遍历和前序遍历得到后序遍历(树的遍历)
2022-08-02 03:22:00 【寒江飞冰】
给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。
输入格式:
输入第一行给出一个正整数N(≤30),是二叉树中结点的个数。第二行给出其中序遍历序列。第三行给出其前序遍历序列。数字间以空格分隔。
输出格式:
在一行中输出该树的后序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。
输入样例:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
输出样例:
3 2 7 6 5 4 1
#include<iostream>
using namespace std;
int a[30];
int b[30];
typedef struct Node{
int data;
Node *lchild;
Node *rchild;
}Node,*Tree;
Tree build(int *a,int *b,int n)
{
if(n<=0)
{
return NULL;
}
int *p=a;
while(p)
{
if(*p==*b) break;
p++;
}
Tree T;
T=new Node;
T->data=*p;
int m=p-a;
T->lchild=build(a,b,m);
T->rchild=build(p+1,b+m,n-m-1);
return T;
}
void Print(Tree T)
{
if(T)
{
Print(T->lchild);
Print(T->rchild);
cout<<' '<<T->data;
}
}
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(a,b,n);
Print(T);
}
边栏推荐
- 每天填坑,精卫填坑第二集,TX1 配置从固态启动,安装Pytorch
- Problems when yolov5 calls ip camera
- nucleo stm32 h743 FREERTOS CUBE MX配置小记录
- How to check whether a table is locked in mysql
- oracle inner join and outer join
- IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boo
- AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
- Debian 10 NTP Service Configuration
- Week 7 Review
- 动态代理工具类
猜你喜欢

DSPE-PEG-DBCO Phospholipid-Polyethylene Glycol-Dibenzocyclooctyne A Linear Heterobifunctional Pegylation Reagent

UserWarning:火炬。meshgrid:在以后的版本中,它将被要求通过索引ing argu

AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'

UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the index ing argu

C语言入门小游戏—三子棋

Week 7 Review

ModuleNotFoundError No module named ‘xxx‘可能的解决方案大全

yolov5调用ip摄像头时出现的问题

【手把手带你学nRF52832/nRF52840 · (1)开发环境搭建】

DSPE-PEG-Silane,DSPE-PEG-SIL,磷脂-聚乙二醇-硅烷修饰活性基团
随机推荐
排序学习笔记(二)堆排序
oracle inner join and outer join
COCO数据集训练TPH-YoloV5
[Mianjing] Mihayou data development on one side and two sides
最新,每天填坑,Jeston TX1 精卫填坑,第一步:刷机
错误:with open(txt_path,‘r‘) as f: FileNotFoundError: [Errno 2] No such file or directory:
np.unique()函数
Detailed explanation of the usage of exists in mysql
远程调试、无cuDnn、自定义模块无法导入问题记录
The @autowired distinguished from @ the Resource
@DateTimeFormat注解
ModuleNotFoundError No module named ‘xxx‘可能的解决方案大全
Monaco Editor 的基本用法
【程序人生】做了多年的运维,靠什么转行拿下12K+年终奖的薪资?
@Accessors 注解详解
Deveco studio 鸿蒙app访问网络详细过程(js)
Knowledge Engineering Assignment 2: Introduction to Knowledge Engineering Related Fields
第一篇博客
Scientific research reagent DMPE-PEG-Mal dimyristoylphosphatidylethanolamine-polyethylene glycol-maleimide
Basic usage of Monaco Editor