二叉树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 ...
随机推荐
- C#高性能数组拷贝实验
前言 昨天 wc(Wyu_Cnk) 提了个问题 C# 里多维数组拷贝有没有什么比较优雅的写法? 这不是问对人了吗?正好我最近在搞图像处理,要和内存打交道,我一下就想到了在C#里面直接像C/C++一样做 ...
- 【教程搬运】分析并编写suricata规则(内含两个示例)
suricata规则分析 参考1 参考2 Suricata 签名的结构 在高层次上,Suricata签名由三部分组成: Action:当流量符合规则时采取的行动 Header:一个标题,描述主机.IP ...
- Django中需要修改配置
Django需要修改配置 1.修改templates中的路径配置: TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.Django ...
- Android面试-字节一面
距离上次跳槽已经过了3年多,突然看到字节的HR来捞.想着自己好久没面了,就打算去试试看. 0. 准备 视频面试,不用去现场真的太赞了.由于项目比较忙,自己又不是特别想跳槽,所以没怎么准备.面试当天看了 ...
- Class path contains multiple SLF4J bindings解决
1.根据控制台查看冲突的日志依赖 本工程Maven依赖 <dependencies> <dependency> <groupId>org.slf4j</gro ...
- 钓鱼攻击之:Flash 网页钓鱼
钓鱼攻击之:Flash 网页钓鱼 目录 钓鱼攻击之:Flash 网页钓鱼 1 准备资料 2 具体钓鱼流程 3 生成后门并捆绑flash的安装程序 4 处理WEB站点过程 1 准备资料 Flash网站克 ...
- .net 定时任务(调度 .net quartz) demo
详细解说:https://blog.csdn.net/noaman_wgs/article/details/80984873 demo如下: 新建控制台应用程式: 添加Nuget包 static vo ...
- kubernetes client-go功能介绍
原味地址 https://haiyux.cc/2023/02/26/k8s-client-go/ client-go是什么? client-go是Kubernetes官方提供的Go语言客户端库,用于与 ...
- Git与GitHub的快速使用
Git的快速使用 1. Git简介 Git最初是Linus花了两周时间自己用C写了一个分布式版本控制系统 特点: 分布式,每一个主机都有完整版本库 开源免费性能好 注:类似GitHub这种中心环境的存 ...
- day09-MyBatis缓存
MyBatis缓存 mybatis – MyBatis 3 | cache MyBatis 一级缓存全详解(一) MyBatis 内置了一个强大的事务性查询缓存机制,它可以非常方便地配置和定制. 为了 ...