二叉树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 ...
随机推荐
- 03初识MapReduce
初识MapReduce 一.什么是MapReduce MapReduce是一种编程范式,它借助Map将一个大任务分解成多个小任务,再借助Reduce归并Map的结果.MapReduce虽然原理很简单, ...
- three.js一步一步来--如何画出一个逃跑的圆柱体
代码如下 <template> <div style="width:1000px; height:800px"> <p>逃跑的圆柱体</p ...
- vue-admin-template 单页面刷新 404 问题
vue-admin-template (vue-element-admin)是个非常好用的B端UI框架,谁用谁知道! 在使用的过程中,加入权限模块后,刷新后权限控制的页面就默认跳转到404页面了,搞了 ...
- Android第六次作业
图片一 用内部存储实现文件写入和读取功能 <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...
- Task记录2.ContinueWith 延续任务,等待上一个任务执行完毕
Task.Run(() => {for (int i = 0; i < 20; i++) { Console.WriteLine(i); } } }).ContinueWith(NewTa ...
- idea debug---启动超级慢,提示”Method breakpoints may dramatically slow down debugging“的解决办法
https://blog.csdn.net/hanqing456/article/details/111878982 1.问题项目正常启动的时候没问题,debug模式就卡住了,很久不动.我推测是哪个断 ...
- P25_wxss - rpx单位
模板样式 - rpx 什么是 rpx 尺寸单位 rpx(responsive pixel)是微信小程序独有的,用来解决屏适配的尺寸单位. rpx 的实现原理 rpx 的实现原理非常简单:鉴于不同设备屏 ...
- .net core 阿里云接口之获取临时访问凭证
假设您是一个移动App开发者,希望使用阿里云OSS服务来保存App的终端用户数据,并且要保证每个App用户之间的数据隔离.此时,您可以使用STS授权用户直接访问OSS. 使用STS授权用户直接访问OS ...
- 为Jekyll静态网站添加PlantUML插件
前言 突然想起来要好好整理一下自己的博客空间,已经荒废很多年,如果再不捡起来,等到自己知识老化的时候再去写东西就没人看了. 使用Github Pages + Jekyll把博客发布为静态网站,给人感觉 ...
- Spring(IOC实际开发使用、底层原理)
实际开发的使用 实际开发中会将程序分为3层: Controller Servlet Repository(DAO) 关系Controller 调运Servlet 调运 Repository(DAO) ...