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. ...
随机推荐
- 统计字符在字符串中第n次出现的位置
输入一个字符串s,一个数字n和一个字符c,统计这个字符c在字符串s中第n次出现的位置 输入格式: 输入3行.第1行是字符串s,第2行是数字n,第3行是被查找的字符c. 输出格式: 第n个字符在字符串中 ...
- 使用Spring框架整合Java Mail
我的博客名为黑客之谜,今天演示的案例中会出现我的邮箱,还不赶紧收藏!我现在是小白,但是随着时间的流逝,我会逐渐向大神走进,所以,喜欢我的,或者喜欢大神的,点一波关注吧!顺便说一下,双十二快到了,有什么 ...
- 「JSOI2016」灯塔
「JSOI2016」灯塔 传送门 我们先只计算照亮左边的灯塔的最低高度,计算右边的类同,然后只要取 \(\max\) 就好了. 那么稍微整理一下式子:\(p_i \ge h_j - h_i + \sq ...
- python web django base skill
web框架本质 socket + 业务逻辑 框架实现socket tonado node.js 使用WSGI实现socket django flask 自己实现框架思路 wsgiref socket ...
- 占位符 css
== 普通的英文半角空格 == == == no-break space (普通的英文半角空格但不换行) == 中文全角空格 (一个中文宽度) == == en空格 (半个中文 ...
- linux使用wget下载https开头url的文件
可以使用命令 : 比如我们可以修改成: wget --no-check-certificate https://我们下载文件路径 来自: http://www.laozuo.org/3648.html
- The Preliminary Contest for ICPC Asia Xuzhou 2019 M. Longest subsequence(思维+序列自动机)
序列自动机跑s串 假设k为s和t相同的长度,初始时相同长度为0 取s串中大于t[i]的最左边的位置,用n-tmp+1+i-1更新答案,tmp是最左端的位置 然后去t[i]相等的位置,走到下一位,如果下 ...
- Codeforces Round #618 E
题意: 给你一个n的数组,你可以进行无数次,选择区间使得区间内的值相加,然后区间的所有的值变成平均值. 使得最后数组的字典序最小 思路: 最后的数组一定是单调递增的,只要它比之前的平均值数大,就不会操 ...
- Strategic game POJ - 1463
题目链接 依旧是树形dp啦,一样的找根节点然后自下而上更新即可 设\(dp_{i,0}\)表示第i个不设,\(dp_{i,1}\)表示第i个设一个,容易得到其状态转移 \(dp_{i,0} = \su ...
- SpringBoot与Shiro整合
一.数据库设计 这里主要涉及到五张表:用户表,角色表(用户所拥有的角色),权限表(角色所涉及到的权限),用户-角色表(用户和角色是多对多的),角色-权限表(角色和权限是多对多的).表结构建立的sq ...