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的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. 二叉树的递归实现(java)

    这里演示的二叉树为3层. 递归实现,先构造出一个root节点,先判断左子节点是否为空,为空则构造左子节点,否则进入下一步判断右子节点是否为空,为空则构造右子节点. 利用层数控制迭代次数. 依次递归第二 ...

  3. c 二叉树的使用

    简单的通过一个寻找嫌疑人的小程序 来演示二叉树的使用 #include <stdio.h> #include <stdlib.h> #include <string.h& ...

  4. Java 二叉树遍历右视图-LeetCode199

    题目如下: 题目给出的例子不太好,容易让人误解成不断顺着右节点访问就好了,但是题目意思并不是这样. 换成通俗的意思:按层遍历二叉树,输出每层的最右端结点. 这就明白时一道二叉树层序遍历的问题,用一个队 ...

  5. 数据结构:二叉树 基于list实现(python版)

    基于python的list实现二叉树 #!/usr/bin/env python # -*- coding:utf-8 -*- class BinTreeValueError(ValueError): ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  8. [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, ...

  9. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  10. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

随机推荐

  1. 在线程里使用线程外的变量为什么一定要是final类型

    public class CyclicBarrierDemo { public static void main(String[] args) { /* * 七龙珠 * */ CyclicBarrie ...

  2. 创建型模式 - 简单工厂模式StaticFactoryMethod

    简单工厂模式的定义         创建型模式:         我们把被创建的对象称为产品,把创建产品的对象称为工厂.如果要创建的产品不多,只要一个工厂类就可以完成,这种模式叫简单工厂模式. 在简单 ...

  3. KingbaseES在线wal日志

    KingbaseES数据库日志文件记录数据库的历史操作信息, 包含恢复数据库中的所有事务所需的信息. KingbaseES在线WAL日志: WAL日志: 预写式日志(Write-Ahead Loggi ...

  4. 多变量两两相互关系联合分布图的Python绘制

      本文介绍基于Python中seaborn模块,实现联合分布图绘制的方法.   联合分布(Joint Distribution)图是一种查看两个或两个以上变量之间两两相互关系的可视化图,在数据分析操 ...

  5. springcloud 10 spring cloud gateway02 基本使用

    官网:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/ 注册 ...

  6. centos7 在线或离线安装python3

    1.前言 本文会使用到yum和wget,如果两者都不能用,参考安装教程 https://www.cnblogs.com/dennisdong/p/17037248.html 2.查看是否安装wget和 ...

  7. gitlabApi如何获取项目文件夹的commitId

      在我们做配置管理系统和gitlab系统集成的时候,有一个常见的场景,就是要获取某个文件的commitId,来记录本次配置文件提交的版本.这个通过gitlabApi很容易实现: GET /proje ...

  8. Elemen ui&表单 、CRUD、安装

    ElementUI表单 Form表单,每一个表单域是由一个form-item组件构成的,表单域中可以放置各种类型的表单控键,有input.switch.checkbox 表单的绑定form 内容分别是 ...

  9. JZOJ 7377.欢乐豆

    \(\text{Problem}\) 有一个有向完全图,所有的 \(u\) 到 \(v\) 的边权为 \(a_u\) 修改 \(m\) 此有向边边权,求最终图上两两点对的最短路之和 \(1\le n ...

  10. Spark系列 - (3) Spark SQL

    3. Spark SQL 3.1 Hive.Shark和Sparksql Hive:Hadoop刚开始出来的时候,使用的是hadoop自带的分布式计算系统 MapReduce,但是MapReduce的 ...