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. ...
随机推荐
- source insight 编译后出现停止工作解决方法
解决方法: 工程的路径不要有中文,都用英文
- Ubuntu18.04配置中文输入法(系统自带的)
Ubuntu18.04配置中文输入法 之前安装Ubuntu18.04的时候选择了english,现在要配置中文的输入法 打开系统设置 点击其中的工具按钮左边第一个 点击管理语言 ==如果系统刚安装可能 ...
- [Bug合集] java.lang.IllegalStateException: Could not find method onClickcrea(View) in a parent
出现场景: 在一个Button中定义了onclick属性,值为startChat. 在Activity中定义一个方法. public void startChat(View view){} 运行时,点 ...
- Cygwin不能编译及解决办法
最近不知道什么原因,以前使用cygwin编译Android动态库,现在不能使用了,报下面的错误. Android NDK: Host 'awk' tool is outdated. Please de ...
- mysql学习笔记(三):unsigned理解以及特殊情况
UNSIGNED UNSIGNED属性就是将数字类型无符号化,与C.C++这些程序语言中的unsigned含义相同.例如,INT的类型范围是-2 147 483 648 - 2 147 483 647 ...
- 具体的client-server通信模型以及最为常用的通信模式
实现虚拟网络服务的主要技术,指出IP负载均衡技术是在负载调度器的实现技术中效率最高的. 在已有的IP负载均衡技术中: 1)有通过网络地址转换(Network Address Translation)将 ...
- Codeforces 1045F Shady Lady 凸包+数学
题目链接:https://codeforc.es/contest/1045/problem/F 题意:先给出一个系数不确定的二元多项式,Borna可以给这个多项式的每一项填上正的系数,Ani能从这个多 ...
- 「JSOI2013」旅行时的困惑
「JSOI2013」旅行时的困惑 传送门 由于我们的图不仅是一个 \(\text{DAG}\) 而且在形态上还是一棵树,也就是说我们为了实现节点之间互相可达,就必须把每条边都覆盖一次,因为两个点之间的 ...
- 创业学习---《预判项目的长期壁垒》--B-3.预判模块---HHR计划---以太一堂
一,<开始学习> 1,投资人经常会问CEO:你的项目的长期壁垒是什么?你是怎么思考的? 2,三个预热思考题: (1)突然有一天,大公司要抄你,你会怎么办? 答:用增长技术来和他竞争. ( ...
- centosflask+uWSGI+nginx部署
centosflask+uWSGI+nginx部署 1. 概念 Flask自带webserver--Werkzeug,可以搭建服务,运行网站.但在开发时,一般会用专业的--uWSGI. 另外 ...