当前位置:网站首页>由中序遍历和后序遍历得到前序遍历(树的遍历)
由中序遍历和后序遍历得到前序遍历(树的遍历)
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);
}
边栏推荐
- nucleo stm32 h743 FREERTOS CUBE MX配置小记录
- yolov5调用ip摄像头时出现的问题
- Phospholipid-polyethylene glycol-azide, DSPE-PEG-Azide, DSPE-PEG-N3, MW: 5000
- IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boo
- COCO数据集训练TPH-YoloV5
- display,visibility,opacity
- ssm various configuration templates
- 磷脂-聚乙二醇-酰肼,DSPE-PEG-Hydrazide,DSPE-PEG-HZ,MW:5000
- @Autowired与@Resource区别
- Redis simple study notes
猜你喜欢

Usage of JOIN in MySQL

MySQL中字符串比较大小(日期字符串比较问题)

kettle 安装与配置

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

Cloud server installation and deployment of Nacos 2.0.4 version

【我的创作纪念日】 3周年

Phospholipid-Polyethylene Glycol-Aldehyde DSPE-PEG-Aldehyde DSPE-PEG-CHO MW: 5000

How to check whether a table is locked in mysql

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

一个结构体 = 另一个结构体(同类型结构体之间可直接赋值操作)
随机推荐
basic operator
PCL—point cloud data segmentation
3分钟带你了解微信小程序开发
磷脂-聚乙二醇-靶向新生血管靶向肽APRPG,DSPE-PEG-APRPG
[Mianjing] Mihayou data development on one side and two sides
kettle 安装与配置
String comparison size in MySQL (date string comparison problem)
C语言 0长度数组/柔性数组
DSPE-PEG-Silane, DSPE-PEG-SIL, phospholipid-polyethylene glycol-silane modified active group
C语言入门小游戏—三子棋
np.isnan()
Debian 10 NTP Service Configuration
The @autowired distinguished from @ the Resource
C语言 内联函数
STM32 map文件解析
UserWarning:火炬。meshgrid:在以后的版本中,它将被要求通过索引ing argu
mysql创建表
getattr() function analysis
MySQL分区表详解
每天填坑,精卫填坑第二集,TX1 配置从固态启动,安装Pytorch