L2-011 玩转二叉树 (25 分) (树)
链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805065406070784
题目:
给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列。所谓镜面反转,是指将所有非叶结点的左右孩子对换。这里假设键值都是互不相等的正整数。
输入格式:
输入第一行给出一个正整数N(≤30),是二叉树中结点的个数。第二行给出其中序遍历序列。第三行给出其前序遍历序列。数字间以空格分隔。
输出格式:
在一行中输出该树反转后的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。
输入样例:
7
1 2 3 4 5 6 7
4 1 3 2 6 5 7
输出样例:
4 6 1 7 5 3 2
思路:
在用中序遍历和前序遍历建树的时候 把左儿子和右儿子的递归交换一下即可建成镜像二叉树
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue> using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int maxn=;
int n;
int pr[maxn],mid[maxn]; struct node{
int l,r;
}T[maxn]; int mid_pr_find(int la,int ra,int lb,int rb){
if(la>ra) return ;
int rt=pr[lb];
int p1=la,len;
while(mid[p1]!=rt) p1++;
len=p1-la;
T[rt].r=mid_pr_find(la,p1-,lb+,lb+len);
T[rt].l=mid_pr_find(p1+,ra,lb+len+,rb);
return rt;
} void bfs(int rt){
queue<int>Q;
vector<int>v;
Q.push(rt);
while(!Q.empty()){
int w=Q.front();
Q.pop();
v.push_back(w);
if(T[w].l!=) Q.push(T[w].l);
if(T[w].r!=) Q.push(T[w].r);
}
int len=v.size();
for(int i=;i<len;i++){
printf("%d%c",v[i],i==(len-)?'\n':' ');
}
} int main(){
scanf("%d",&n);
for(int i=;i<n;i++) scanf("%d",&mid[i]);
for(int i=;i<n;i++) scanf("%d",&pr[i]);
int rt=mid_pr_find(,n-,,n-);
bfs(rt);
return ;
}
L2-011 玩转二叉树 (25 分) (树)的更多相关文章
- PTA 7-1 还原二叉树 (25分)
		PTA 7-1 还原二叉树 (25分) 给定一棵二叉树的先序遍历序列和中序遍历序列,要求计算该二叉树的高度. 输入格式: 输入首先给出正整数N(≤50),为树中结点总数.下面两行先后给出先序和中序遍历 ... 
- 1043 Is It a Binary Search Tree (25分)(树的插入)
		A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ... 
- L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)
		题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ... 
- PTA 03-树1 树的同构   (25分)
		题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/711 5-3 树的同构 (25分) 给定两棵树T1和T2.如果T1可以通过若干次左右 ... 
- PTA 树的同构 (25分)
		PTA 树的同构 (25分) 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号):随后N行,第i行对应编号第 ... 
- PTA 7-3 树的遍历 (25分)
		PTA 7-3 树的遍历 (25分) 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤30),是二叉树中结点 ... 
- 团体程序设计天梯赛 L2-006. 树的遍历 L2-011. 玩转二叉树
		L2-006. 树的遍历 #include <stdio.h> #include <stdlib.h> #include <string.h> #include & ... 
- PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)
		1021 Deepest Root (25 分) A graph which is connected and acyclic can be considered a tree. The heig ... 
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
		1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ... 
- PTA  根据后序和中序遍历输出先序遍历(25 分)
		7-1 根据后序和中序遍历输出先序遍历(25 分) 本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的先序遍历结果. 输入格式: 第一行给出正整数N(≤30),是树中结点的个数.随后两行 ... 
随机推荐
- wince可用的7-zip
			7-zip下载 7-zip 
- svg-sprite使用
			chainWebpack(config) { config.module .rule('svg') .exclude.add(path.resolve(__dirname,'src/assets/ic ... 
- VS2013下载与安装
			1. 官网下载地址: https://my.visualstudio.com/Downloads?q=visual studio 2013&wt.mc_id=o~msft~vscom~old ... 
- SpringBoot+Swagger整合API
			SpringBoot+Swagger整合API Swagger:整合规范的api,有界面的操作,测试 1.在pom.xml加入swagger依赖 <!--整合Swagger2配置类--> ... 
- tensorflow-TensorBoard
			Tensorborad--> 是Tensorflow的可视化工具,它可以通过Tensorflow程序运行过程中输出的日志文件可视化Tensorflow程序的运行状态.Tensorflow和Ten ... 
- Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array 分类讨论连续递推dp
			题意:给出一个 数列 和一个x 可以对数列一个连续的部分 每个数乘以x 问该序列可以达到的最大连续序列和是多少 思路: 不是所有区间题目都是线段树!!!!!! 这题其实是一个很简单的dp 使用的是分 ... 
- Flask 构建微电影视频网站(八)
			评论收藏及弹幕 实现电影评论添加及列表.数据查询实现统计播放量和评论量.jquery ajax实现收藏电影,flask结合redis消息队列实现电影弹幕,bug处理等功能. 电影评论-统计 class ... 
- SWIG 扩展Opencv python调用C++
			osx:10.12 g++ 7.1 swig 3.0.12 opencv 3.2.0 SWIG是Simplified Wrapper and Interface Generator的缩写.是Pytho ... 
- shc & unshc 安装
			shc & unshc 安装 shc 安装 git clone https://github.com/neurobin/shc.git cd shc ./configure make & ... 
- Android Intent 传递数据注意事项
			不要通过 Intent 在 Android 基础组件之间传递大数据(binder transaction缓存为 1MB),可能导致 OOM. 
