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),是树中结点的个数.随后两行 ...
随机推荐
- MATLAB GUI界面设计------“轴”组件配置
1> Fontsize 10 %字体大小 2> FontUnits normalized %采用相对度量单位,缩放时保持 ...
- CSS 实现自动换行、强制换行、强制不换行的属性
实现效果 1.自动换行: word-wrap:break-word; word-break:normal; 2.强制换行: word-break:break-all; 按字符截断换行 /* ...
- MySQL存储过程--(1)
/*参数模式:IN:该参数作为输入,该参数调用传入值out:该参数作为输出,该参数作为返回值INOUT:该参数即可作为输入,也可作为输出,该参数即可调用传入值,也可作为返回值delimiter:设置结 ...
- 企微云CRM操作指南 – 道一云|企微
企微云CRM操作指南 – 道一云|企微https://wbg.do1.com.cn/xueyuan/2568.html 线索及线索池 – 道一云|企微https://wbg.do1.com.cn/xu ...
- 在windows环境利用celery实现简单的任务队列
测试使用环境: 1.Python==3.6.1 2.MongoDB==3.6.2 3.celery==4.1.1 4.eventlet==0.23.0 Celery分为3个部分 (1)worker部分 ...
- Keil MDK5的ITM调试
https://blog.csdn.net/burgesskzg/article/details/77100453
- Leetcode 4.28 Tree Easy
1. 101. Symmetric Tree 用递归. class Solution { public boolean isSymmetric(TreeNode root) { if( root == ...
- Android面试题集合
https://www.jianshu.com/p/718aa3c1a70b https://www.jianshu.com/p/2dd855aa1938 https://www.jianshu.co ...
- cyq.data 常见使用方法
配置数据库链接:(这只是其中一种方式) AppConfig.DB.CommandTimeout = 800; AppConfig.DB.DefaultConn = "数据库链接地址" ...
- [SMB share]Create SMB share under powershell / poweshell下创建本机的SMB共享
New-SmbShare -Name share-name -Path C:\share -FolderEnumerationMode AccessBased -CachingMode Documen ...