给出一棵二叉树的后序遍历序列和中序遍历序列,求这棵二叉树的层序遍历序列

#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std; const int maxn = 50;
struct node
{
int data;
node* lchild;
node* rchild;
}; int pre[maxn],in[maxn],post[maxn];//先序,中序,后序
int n;//结点个数
//当前二叉树的后序序列区间为[postl,postr],中序序列的区间为[inl,inr]
//create函数返回构建出的二叉树的根节点 node* create(int postL,int postR,int inL,int inR)
{
if(postL>postR)
{
return NULL;//后序序列长度小于等于1时直接返回
}
node* root =new node;//新建一个节点,用来存放当前二叉树的根节点
root->data =post[postR];//新节点的数据域为根节点的值
int k;
for(k=inL;k<=inR;k++)
{
if(in[k]==post[postR])
{
break;
}
}
int numLeft = k-inL;//左子树节点的数目
//返回左子树的根节点的地址,赋值给root的左指针
root->lchild = create(postL,postL+numLeft-1,inL,k-1);
//返回右子树的根节点的地址,赋值给root的右指针
root->rchild = create(postL+numLeft,postR-1,k+1,inR);
return root;//返回根节点的地址
} int num =0; //已经输出的结点的个数
void BFS(node* root)
{
queue<node*> q;//队列里面是存放地址
q.push(root);//将根节点的地址入队
while(!q.empty())
{
node* now = q.front();//取出队首元素
q.pop();
printf("%d",now->data);//访问队首元素
num++;
if(num<n) printf(" ");
if(now->lchild != NULL) q.push(now->lchild);//左子树非空
if(now->rchild != NULL) q.push(now->rchild);//右子树
}
} int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&post[i]);
}
for(int i=0;i<n;i++)
{
scanf("%d",&in[i]);
}
node* root = create(0,n-1,0,n-1);//建树
BFS(root); //层序遍历
return 0;
}

  

PAT A 1020 Tree Traversals的更多相关文章

  1. 【PAT】1020 Tree Traversals (25)(25 分)

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  2. PAT 甲级 1020 Tree Traversals (二叉树遍历)

    1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

  3. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  4. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  5. PAT Advanced 1020 Tree Traversals (25 分)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  6. 【PAT】1020. Tree Traversals (25)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  7. PAT 甲级 1020 Tree Traversals

    https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072 Suppose that all the k ...

  8. PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

  9. PAT 1020. Tree Traversals

    PAT 1020. Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. ...

随机推荐

  1. Python学习之列表篇

    浮点数类型:round(x,d)可对浮点数进行四舍五入,科学计数法:aeb表示a*10^bpython大小写敏感整数类型:无范围限制,pow(x,y)表示x^y,想算多大算多大,四种表示形式:十进制, ...

  2. 服务器部署网站后,公网ip可以访问,域名不能访问问题(稳)

    出现问题 这几天我网站已经部署到vps上,域名也备好案,想使用域名指向我们公网ip.指完发现用域名访问不了网站,但是公网ip可以.于是看了网上资料,好像是要清除浏览器DNS缓存,我清完没用.然后发现我 ...

  3. 对DensePose: Dense Human Pose Estimation In The Wild的理解

    研究方法 通过完全卷积学习从图像像素到密集模板网格的映射.将此任务作为一个回归问题,并利用手动注释的面部标注来训练我们的网络.使用这样的标注,在三维对象模板和输入图像之间,建立密集的对应领域,然后作为 ...

  4. 洛谷 P1880 [NOI1995]石子合并(区间DP)

    嗯... 题目链接:https://www.luogu.org/problem/P1880 这道题特点在于石子是一个环,所以让a[i+n] = a[i](两倍长度)即可解决环的问题,然后注意求区间最小 ...

  5. SpringMVC Controller 接收页面传递的中文参数出现乱码

    在Controller中接收到的POST参数如果是中文的话,显示为乱码.已知客户端传过来时编码为UTF-8. 问题产生分析: spring MVC中默认的编码格式为“ISO-8859-1”,因此造成乱 ...

  6. React项目中遇到的那些坑

    1.react中路由跳转后页面不置顶问题 问题: 从页面A跳转到页面B,页面A滚动到中间位置,跳转后页面B也会在中间位置 解决方法:在顶部组件的生命周期中进行判断,例如 componentWillRe ...

  7. go.php

    <?php $t_url=$_GET['url']; if(!empty($t_url)) { preg_match('/(http|https):\/\//',$t_url,$matches) ...

  8. Dapr Pub/Sub 集成 RabbitMQ 、Golang、Java、DotNet Core

    前置条件: <Dapr运用> <Dapr 运用之 Java gRPC 调用篇> <Dapr 运用之集成 Asp.Net Core Grpc 调用篇> 搭建 Rabb ...

  9. BeanUtils学习笔记

    一. 简介 BeanUtils提供对Java反射和自省API的包装.其主要目的是利用反射机制对JavaBean的属性进行简化操作处理.一个JavaBean通常包含了大量的属性,很多情况下,对JavaB ...

  10. <img>标签显示本地路径的图片的.NET解决方案

    今天朋友问了我一个奇怪的需求:项目中要求图片上传到工作目录,上传后要在网页中通过<img>显示出来.图片上传后显示,在开发中常见的做法是将它图片上传到网站目录下(upload/),如果保存 ...