二叉树TwT
L2-011 玩转二叉树
给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列。所谓镜面反转,是指将所有非叶结点的左右孩子对换。这里假设键值都是互不相等的正整数。
输入格式:
输入第一行给出一个正整数N(≤30),是二叉树中结点的个数。第二行给出其中序遍历序列。第三行给出其前序遍历序列。数字间以空格分隔。
输出格式:
在一行中输出该树反转后的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。
由中序遍历和前序遍历构造一个二叉树:
int BuildingTree(int l1,int r1,int l2,int r2)
{
if(l1>r1||l2>r2)
return 0;
int p=l1;
while(per[l2]!=in[p])
p++;
int len=p-l1;
int rt=per[l2];
tree[rt].l=BuildingTree(l1,p-1,l2-1,l2+len);
tree[rt].r=BuildingTree(p+1,r1,l2+1+len,r2);
return rt;
}
代码:
#include<bits/stdc++.h>
using namespace std;
int n,in[1010],per[1010];
struct Tree
{
int l,r;
}tree[1010];
int BuildingTree(int l1,int r1,int l2,int r2)
{
if(l1>r1||l2>r2)
return 0;
int p=l1;
while(per[l2]!=in[p])
p++;
int len=p-l1;
int rt=per[l2];
tree[rt].l=BuildingTree(l1,p-1,l2+1,l2+len);
tree[rt].r=BuildingTree(p+1,r1,l2+1+len,r2);
return rt;
}
void bfs(int s)
{
int cnt=0;
queue<int>q;
q.push(s);
while(!q.empty())
{
int rt=q.front();
q.pop();
cout<<rt<<" \n"[++cnt==n];
int l=tree[rt].l;
int r=tree[rt].r;
if(r) q.push(r);
if(l) q.push(l);
}
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&in[i]);
for(int i=0;i<n;i++)
scanf("%d",&per[i]);
BuildingTree(0,n-1,0,n-1);
bfs(per[0]);
return 0;
}
题解链接,作者真的好聪明啊QAQ
1.题解为什么是正确的:
确实是正确的。
2.看到题目为什么要向这方面思考,得出这样的思路:
思路有,代码不会实现,作者代码实现好巧妙,这个怎么学呢
3.学到了啥
我再想想
二叉树TwT的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- 二叉树的递归实现(java)
这里演示的二叉树为3层. 递归实现,先构造出一个root节点,先判断左子节点是否为空,为空则构造左子节点,否则进入下一步判断右子节点是否为空,为空则构造右子节点. 利用层数控制迭代次数. 依次递归第二 ...
- c 二叉树的使用
简单的通过一个寻找嫌疑人的小程序 来演示二叉树的使用 #include <stdio.h> #include <stdlib.h> #include <string.h& ...
- Java 二叉树遍历右视图-LeetCode199
题目如下: 题目给出的例子不太好,容易让人误解成不断顺着右节点访问就好了,但是题目意思并不是这样. 换成通俗的意思:按层遍历二叉树,输出每层的最右端结点. 这就明白时一道二叉树层序遍历的问题,用一个队 ...
- 数据结构:二叉树 基于list实现(python版)
基于python的list实现二叉树 #!/usr/bin/env python # -*- coding:utf-8 -*- class BinTreeValueError(ValueError): ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化
One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
随机推荐
- JavaScript所有内部属性列表 [[Configurable]] 等
简介 据MDN文档所说内部属性是由 [[···]] 包裹的内容,于是我们去复制 ECMA-262 标准的所有文字部分,然后用正则统计 [[···]] 的出现并打印 效果 代码 经老大提醒,这里的正则并 ...
- 力扣每日一题2023.1.16---1813. 句子相似性 III
一个句子是由一些单词与它们之间的单个空格组成,且句子的开头和结尾没有多余空格.比方说,"Hello World" ,"HELLO" ,"hello w ...
- UBUNTU18.04安装使用ORB-SLAM2
1.下载: git clone https://github.com/raulmur/ORB_SLAM2.git ORB_SLAM2 2.依赖项: sudo apt install autotools ...
- LG P2633 Count on a tree
\(\text{Solution}\) 树上主席树板子 \(\text{Code}\) #include <cstdio> #include <algorithm> #defi ...
- 跳板攻击之:lcx 端口转发
跳板攻击之:lcx 端口转发 郑重声明: 本笔记编写目的只用于安全知识提升,并与更多人共享安全知识,切勿使用笔记中的技术进行违法活动,利用笔记中的技术造成的后果与作者本人无关.倡导维护网络安全人人有责 ...
- 自定义顺序表ArrayList
1.简介 顺序表是在计算机内存中以数组的形式保存的线性表,线性表的顺序存储是指用一组地址连续的存储单元依次存储线性表中的各个元素.使得线性表中在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中,即通 ...
- 手机在线编程软件Anycodes
下载地址:http://ys-f.ys168.com/608949803/V6gUhjk3V382275HM63/%E6%89%8B%E6%9C%BA%E7%BC%96%E7%A8%8B%E8%BD% ...
- C# HttpClient 上传大文件带进度
在Httpclient 上传文件时 需要显示进度,需要添加 ProgressMessageHandler 在NuGet中添加 引用Microsoft.AspNet.WebApi.Client 一下是 ...
- org.elasticsearch.ElasticsearchException: not all primary shards of [.geoip_databases] index are active解决办法
解决办法 在配置elasticsearch.yml中加上 ingest.geoip.downloader.enabled: false
- virtualbox装配fedora时,安装增强功能包时会报错解决
virtualbox安装fedora时,安装增强功能包时会报错解决 Building the main GuestAdditions module [失败]安装前需要先安装下面几个包才可以避免这个问题 ...