当前位置:网站首页>由中序遍历和后序遍历得到前序遍历(树的遍历)
由中序遍历和后序遍历得到前序遍历(树的遍历)
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);
}
边栏推荐
- Phospholipid-Polyethylene Glycol-Aldehyde DSPE-PEG-Aldehyde DSPE-PEG-CHO MW: 5000
- How to check whether a table is locked in mysql
- Basic usage of Monaco Editor
- MySql创建数据表
- The @autowired distinguished from @ the Resource
- sh: 1: curl: not found
- cross-domain problem solving
- JJWT tool class
- Deveco studio Hongmeng app access network detailed process (js)
- 配置mmdet来训练Swin-Transformer之一配置环境
猜你喜欢
ModuleNotFoundError No module named ‘xxx‘可能的解决方案大全
subprocess.CalledProcessError: Command 'pip install 'thop'' returned non-zero exit status 1.
String comparison size in MySQL (date string comparison problem)
科研试剂DMPE-PEG-Mal 二肉豆蔻酰磷脂酰乙醇胺-聚乙二醇-马来酰亚胺
AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'
【深度学习】从LeNet-5识别手写数字入门深度学习
Chemical reagent Phospholipid-polyethylene glycol-hydroxyl, DSPE-PEG-OH, DSPE-PEG-Hydroxyl, MW: 5000
Phospholipid-polyethylene glycol-hydrazide, DSPE-PEG-Hydrazide, DSPE-PEG-HZ, MW: 5000
Week 7 Review
【手把手带你学nRF52832/nRF52840 · (1)开发环境搭建】
随机推荐
oracle内连接和外连接
display,visibility,opacity
Chapter 10 Clustering
SOCKS5
UserWarning:火炬。meshgrid:在以后的版本中,它将被要求通过索引ing argu
require模块化语法
oracle inner join and outer join
js 取字符串中某位置某特征的值,如华为(Huawei)=>华为
Chemical reagent Phospholipid-polyethylene glycol-hydroxyl, DSPE-PEG-OH, DSPE-PEG-Hydroxyl, MW: 5000
活体检测 Adaptive Normalized Representation Learning for GeneralizableFace Anti-Spoofing 阅读笔记
UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the index ing argu
磷脂-聚乙二醇-叠氮,DSPE-PEG-Azide,DSPE-PEG-N3,MW:5000
磷脂-聚乙二醇-酰肼,DSPE-PEG-Hydrazide,DSPE-PEG-HZ,MW:5000
Redis简单学习笔记
Error: with open(txt_path,'r') as f: FileNotFoundError: [Errno 2] No such file or directory:
DSPE-PEG-Silane, DSPE-PEG-SIL, phospholipid-polyethylene glycol-silane modified active group
Phospholipid-polyethylene glycol-hydrazide, DSPE-PEG-Hydrazide, DSPE-PEG-HZ, MW: 5000
String comparison size in MySQL (date string comparison problem)
@Autowired与@Resource区别
【深度学习】从LeNet-5识别手写数字入门深度学习