PAT A 1020 Tree Traversals
给出一棵二叉树的后序遍历序列和中序遍历序列,求这棵二叉树的层序遍历序列
#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的更多相关文章
- 【PAT】1020 Tree Traversals (25)(25 分)
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- 【PAT】1020. Tree Traversals (25)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- PAT 甲级 1020 Tree Traversals
https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072 Suppose that all the k ...
- PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- PAT 1020. Tree Traversals
PAT 1020. Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. ...
随机推荐
- Springmvc-crud-02错误(添加出现中文乱码)
错误: 在进行添加页面时使用post请求,输入中文时会出现乱码 原因: post请求不支持gbk格式,使用字符编码过滤器,设置为UTF-8编码即可 注意配置请求的字符集和响应字符集 解决方案:需要放在 ...
- 华硕笔记本(i76700hq+nvidia goforce940mx)安装ubuntu18.04
Ubuntu的安装 今天终于下定决心要把笔记本安装成Ubuntu,但是网上的教材不够全面,我就想整合以下教程. 接下来详细讲解安装过程 1. Ubuntu iso镜像下载 下载地址:https://w ...
- [Python] Tkinter的食用方法_01_简单界面
#开始 放假之后感觉整个人已经放飞自我了,完全不知道自己一天天在干什么,明明有很多的事情需要做,但是实际上每天啥都没做,,,虚度光阴... 晚上突然心烦意乱,开始思考今天一天都做了什么,感觉很有负罪感 ...
- SmartRobotControlPlateform——智能机器人控制平台
具体成果参考github项目:https://github.com/ecjtuseclab/SmartRobotControlPlateform 这里我使用的镜像是:2018-11-13-raspbi ...
- cmake使用的一些补充
一般使用cmake生成vs项目的时候,要么生成32位的要么生成64位的. 怎样将32位和64位在一个工程中打开呢,联系我们自己建立的工程都是32位和64位在一起的,就动手开始了. 实验对象是openc ...
- ListView 基础列表组件、水平 列表组件、图标组件
一.Flutter 列表组件概述 列表布局是我们项目开发中最常用的一种布局方式.Flutter 中我们可以通过 ListView 来定义 列表项,支持垂直和水平方向展示.通过一个属性就可以控制列表的显 ...
- 基于SILVACO ATLAS的a-IGZO薄膜晶体管二维器件仿真(03)
今天逛ResearchGate的时候发现了一个不错的Atlas入门教程:Step by step with ATLAS Silvaco点击链接免费下载.. Atlas代码结构 当然可能有一点太基础了. ...
- 局域网内Linux下开启ftp服务的“曲折路”和命令复习
今天主要学习了Linux下网络配置以及vsftp(FTP)和samba的服务配置,学习起来,难度也就一般,并没有特别难,可是在可以做实验的时候,却并没有自己想像的那么顺利,可见,很多事情看起来不难,做 ...
- 吴裕雄--天生自然Numpy库学习笔记:NumPy 从已有的数组创建数组
import numpy as np x = [1,2,3] a = np.asarray(x) print (a) import numpy as np x = (1,2,3) a = np.asa ...
- 吴裕雄 python 神经网络——TensorFlow 图像预处理完整样例
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt def distort_color(image, ...