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. IntelliJ中高效重构的 10 个快捷方式

    前言 在日常的开发工作中,我们经常需要重构,重构可以让我们写出的代码更上一层楼.所以,我会借助IntelliJ提供的一些功能,帮助我高效进行重构.这里是我推荐10个快捷方式,也是我每天都在使用的,非常 ...

  2. 记一次线上FGC问题排查

    引言 本文记录一次线上 GC 问题的排查过程与思路,希望对各位读者有所帮助.过程中也走了一些弯路,现在有时间沉淀下来思考并总结出来分享给大家,希望对大家今后排查线上 GC 问题有帮助. 背景 服务新功 ...

  3. try catch finally,try里有return,finally还执行么?

    执行,并且finally的执行早于try里面的return: 结论: 1.不管有木有出现异常,finally块中代码都会执行: 2.当try和catch中有return时,finally仍然会执行: ...

  4. 前端基础知识-react(一)个人学习记录 _

    待补充 reactive和ref

  5. FAQ 关于allure和pycharm的运行模式

    关于allure和pycharm的运行模式 案例 新建一个项目allure_mode 新建一个python代码test_allure_001.py 代码如下 import pytest, os def ...

  6. rust 模块、路径、项目类型等相关内容

    rust 模块路径 转载 https://blog.csdn.net/wowotuo/article/details/107591501 rust 项目编译类型 转载 https://blog.51c ...

  7. 点击获取Cesium中加载的3DTile模型的属性信息(二)

    1.添加鼠标点击事件 //鼠标单击左键事件 viewer.screenSpaceEventHandler.setInputAction(function onMouseClick( click ) { ...

  8. JZOJ 2020.07.27【NOIP提高组】模拟

    总结 今天的题还好,不幸拿了 \(rank1\),只有 \(380pts\) 实际 \(AK\) 也不难 前三题都是思维题 后面一题其实不过是简单的数据结构优化 \(dp\) 的题 用 \(GSM\) ...

  9. K3S系列文章-使用AutoK3s在腾讯云上安装高可用K3S集群

    开篇 <K3s 系列文章> <Rancher 系列文章> 方案 在腾讯云上安装 K3S 后续会在这套 K3S 集群上安装 Rancher 方案目标 高可用 3 台master ...

  10. 权限维持之:DSRM 域控权限维持

    目录 1 修改 DSRM 密码 2 DSRM 域后门操作过程 3 DSRM 域后门防御 目录服务恢复模式(DSRM,Directory Services Restore Mode),是Windows服 ...