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

#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. 如何安装和使用Maven

    今天我们来学习一下如何安装Maven,把步骤分享给大家,希望能对大家有帮助! 我的博客地址:https://www.cnblogs.com/themysteryofhackers/p/11996550 ...

  2. opencv:Mat对象

    Mat对象:图像文件的内存数据对象 读取为 Mat 对象 读取图像位Mat对象,获取图像的相关信息 #include <opencv2/opencv.hpp> #include <i ...

  3. pipreqs (找当前项目依赖的包)

    pipreqs pipreqs可以帮你找到当前项目的所有组件及其版本.就是当别人给你一个程序的时候,你要在自己电脑上运行起来,就需要安装程序所依赖的组件,总不能自己一个一个找吧. # 安装 pip3 ...

  4. 【visio】 设计

    1."设计" 包含了 页面.布局和主题相关设置 2."页面设置" 包含:打印.绘制区域.打印区域.页面缩放.页属性以及替换文字. 替换文字 放在页面设置里,这个 ...

  5. android nfc功能开发

    链接:Android NFC开发详细总结   https://blog.csdn.net/zhwadezh/article/details/79111348 链接2:Android NFC功能 简单实 ...

  6. UTC/GMT/CST/RTC

    GMT:格林尼治标准时间,是指位于伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线.也就是零时区的时间. UTC:世界协调时间,是一个时间系统.可以理解为这个地球的标准时间 ...

  7. Java面向对象封装优化1_this(Python中的self)

    1. 类 package cn.itcast.day06.demo03; /* 问题描述:定义Person的年龄时,无法阻止不合理的数值被设置进来. 解决方案:用private关键字将需要保护的成员变 ...

  8. Draw.io--自认为最好用的流程图绘制软件

    draw.io 是一个强大简洁的在线的绘图网站,支持流程图,UML图,架构图,原型图等图标.支持Github,Google Drive, One drive等网盘同步,并且永久免费.如果觉得使用Web ...

  9. mysql中utf8字符集的设置及character_set_database属性修改

    mariadb配置文件修改字符集: [mysqld] atadir=/usr/local/mysql/datasocket=/tmp/mysql.sock # Disabling symbolic-l ...

  10. LeetCode练题——88. Merge Sorted Array

    1.题目 88. Merge Sorted Array——Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into  ...