当前位置:网站首页>由中序遍历和前序遍历得到后序遍历(树的遍历)
由中序遍历和前序遍历得到后序遍历(树的遍历)
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);
}
边栏推荐
猜你喜欢

MySQL分区表详解

MySql创建数据表

The @autowired distinguished from @ the Resource

磷脂-聚乙二醇-叠氮,DSPE-PEG-Azide,DSPE-PEG-N3,MW:5000

parser = argparse.ArgumentParser() parsing

Chapter 10 Clustering
![[Basic Tutorial of Remote Control Development 1] Crazy Shell Open Source Formation Drone-GPIO (Remote Control Indicator Light Control)](/img/da/5f6b1ed89cb3fed3cbe6aea840f473.png)
[Basic Tutorial of Remote Control Development 1] Crazy Shell Open Source Formation Drone-GPIO (Remote Control Indicator Light Control)

MySQL分库分表

【深度学习】从LeNet-5识别手写数字入门深度学习

磷脂-聚乙二醇-靶向新生血管靶向肽APRPG,DSPE-PEG-APRPG
随机推荐
Redis简单学习笔记
sh: 1: curl: not found
知识问答与知识会话的区别
Deveco studio 鸿蒙app访问网络详细过程(js)
一个结构体 = 另一个结构体(同类型结构体之间可直接赋值操作)
np.isnan ()
PCL—点云数据分割
parser = argparse.ArgumentParser() parsing
磷脂-聚乙二醇-巯基,DSPE-PEG-Thiol,DSPE-PEG-SH,MW:5000
Cloud server installation and deployment of Nacos 2.0.4 version
Amazon sellers how to improve the conversion
MySQL分库分表
URL模块
远程调试、无cuDnn、自定义模块无法导入问题记录
The @autowired distinguished from @ the Resource
Scientific research reagent DMPE-PEG-Mal dimyristoylphosphatidylethanolamine-polyethylene glycol-maleimide
ssm various configuration templates
C语言 void和void *(无类型指针)
Week 7 Review
npm和package.json