PAT L2-011 玩转二叉树(二叉树层序遍历)
给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列。所谓镜面反转,是指将所有非叶结点的左右孩子对换。这里假设键值都是互不相等的正整数。
输入格式:
输入第一行给出一个正整数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
题意
给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列。所谓镜面反转,是指将所有非叶结点的左右孩子对换。这里假设键值都是互不相等的正整数
题解
首先建树(一个子树里先序第一个为根,中序里根的左为左子树,右为右子树)
镜面反转其实就是层序遍历先把右孩子推进队列,在把左孩子推进队列
这是按权值作为节点编号(这里有点缺陷,权值很大就GG了)
代码
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100
int In[MAXN],Per[MAXN],Left[MAXN],Right[MAXN],n;
int build(int L1,int R1,int L2,int R2)
{
if(L1>R1)return ;
int root=Per[L2];
int p=L1;
while(In[p]!=root)p++;
int cnt=p-L1;
Left[root]=build(L1,p-,L2+,L2+cnt);
Right[root]=build(p+,R1,L2+cnt+,R2);
return root;
}
void bfs()
{
int ans=;
queue<int> qu;
qu.push(Per[]);
while(!qu.empty())
{
int top=qu.front();qu.pop();
printf("%d%c",top,++ans==n?'\n':' ');
if(Right[top])qu.push(Right[top]);
if(Left[top])qu.push(Left[top]);
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",&In[i]);
for(int i=;i<n;i++)
scanf("%d",&Per[i]);
build(,n-,,n-);
bfs();
return ;
}
PAT L2-011 玩转二叉树(二叉树层序遍历)的更多相关文章
- 二叉树的层序遍历 BFS
二叉树的层序遍历,或者说是宽度优先便利,是经常考察的内容. 问题一:层序遍历二叉树并输出,直接输出结果即可,输出格式为一行. #include <iostream> #include &l ...
- Leetcode 102. Binary Tree Level Order Traversal(二叉树的层序遍历)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 剑指offer 二叉树的层序遍历
剑指offer 牛客网 二叉树的层序遍历 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 09:33:16 2019 @ ...
- leetcode之二叉树的层序遍历
1.题目描述 2.题目分析 二叉树的层序遍历主要算法思想是使用 队列这一数据结构实现,这个数据结构多应用在和 图相关的算法.例如图的广度优先遍历就可以使用队列的方法实现.本题的关键在于如何识别出一层已 ...
- LeetCode 102. 二叉树的层序遍历 | Python
102. 二叉树的层序遍历 题目来源:https://leetcode-cn.com/problems/binary-tree-level-order-traversal 题目 给你一个二叉树,请你返 ...
- 刷题-力扣-107. 二叉树的层序遍历 II
107. 二叉树的层序遍历 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/binary-tree-level-order-tr ...
- 五三想休息,今天还学习,图解二叉树的层序遍历BFS(广度优先)模板,附面试题题解
壹 ❀ 引 我在从JS执行栈角度图解递归以及二叉树的前.中.后遍历的底层差异一文中,从一个最基本的数组遍历引出递归,在掌握递归的书写规则后,又从JS执行栈角度解释了二叉树三种深度优先(前序.中序后序) ...
- leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- 剑指Offer21 二叉树的层序遍历
/************************************************************************* > File Name: 21_PrintT ...
随机推荐
- leetcode 错误题解,反面教材 Gas Station
class Solution { public: int canCompleteCircuit(vector<int>& gas, vector<int>& c ...
- python反汇编函数字节码
使用dis模块 >>> def test(): ... print(1) ... a = 1 ... print(a) ... >>> from dis impor ...
- TypeScript语言学习笔记(1)
基本类型 // 布尔型(Boolean) let isDone: boolean = false; // 数值型(Number) let decimal: number = 6; let hex: n ...
- ArcGIS案例学习笔记3_1_ArcMap编辑练习
ArcGIS案例学习笔记3_1_ArcMap编辑练习 计划时间:第三天上午 目的:ArcMap编辑练习 教程: pdf page67 数据: gis_ex10/ex07 方法: 1.新建shp文件 目 ...
- maven 创建war类型项目
1. 创建maven project时选择packaging为war 2. 在webapp文件夹下新建META-INF和WEB-INF/web.xml 3. 在pom.xml中添加web项目需要的ja ...
- HTTPConnectionPool(host:XX)Max retries exceeded with url
爬虫多次访问同一个网站一段时间后会出现错误 HTTPConnectionPool(host:XX)Max retries exceeded with url '<requests.package ...
- 学JS的心路历程-物件与原型(一)
前两天说明面向对象的三大特性及JS不符合面向对象,只能称作支持面向对象而已,今天我们来看看JS的原型继承. 首先我们先来看,什么是原型(vmwork): 两个物件之间的原型关系(prototype r ...
- bootstrap 参考文档
https://getbootstrap.com/docs/3.3/css/#sass-installation
- ofstream和ifstream
ofstream(输出流)是从内存到硬盘,ifstream(输入流)是从硬盘到内存. //#include<iostream> #include<fstream> using ...
- rem是如何实现自适应布局的
原文链接:http://caibaojian.com/web-app-rem.html 摘要:rem是相对于根元素<html>,这样就意味着,我们只需要在根元素确定一个px字号,则可以来算 ...