二叉树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 ...
随机推荐
- 基于windows系统使用GNVM进行node切换版本
GNVM是什么? GNVM 是一个简单的 Windows 下 Node.js 多版本管理器,类似的 nvm nvmw nodist . 安装 进入官网,下载你所需要的包,直达链接 下载完成 放到我们的 ...
- 【Android 4.4】内存文件系统(tmpfs)的创建与使用
前言说明 某些情况下,需要缓存一些文件到磁盘中,我们可以借助 tmpfs 文件系统,来提升读写缓存文件的速度,并且也可以避免频繁读写缓存文件所带来的对 flash 的寿命影响. 使用方法 通过 mkd ...
- Spring中常见的注解
1.组件注解 @Controller @Service @Repository @Component ---标注一个类为Spring容器的Bean @Configration ---声明当前类为配置类 ...
- Python内置对象(一)
Python内置对象(一) 分多次讲解 这部分相对比较简单,偶尔一些特殊的做法会强调下(前面加★) 总览 builtins = [_ for _ in dir(__builtins__) if not ...
- Flex布局专题
Flex布局专题 参照 https://www.runoob.com/w3cnote/flex-grammar.html 下面是自己看代码的一下 小结,和认识,加笔记,加原文 认识容器 flex布局需 ...
- .net core 删除指定路径下的所有文件以及文件夹(文件夹建议保留目录)
1.服务层 /// <summary> /// 删除指定路径下的所有文件 /// </summary> /// <param name="filepath&qu ...
- vue @click的stop和prevent
@click.stop 阻止事件冒泡 @click.prevent 阻止事件的默认行为 联合饿了吗UI使用的时候,el-table(主表)包含一个或多个子表时(el-tabs),点击右侧的编辑.删除时 ...
- webpack核心用法,为什么要使用webpack
一:为什么使用webpack 1. 代码转换.文件优化.代码分割.模块合并.自动刷新.等等 2. webpack上手 <!DOCTYPE html> <html lang=" ...
- JZOJ 100149. 一道联赛A题
\(\text{Solution}\) 一眼 \(ODT\) 为避免每次都数颜色数量,提前记录下来,每次修改更新下 \(\text{Code}\) #include <cstdio> #i ...
- TEMPO研究第一年影像学数据: 骨侵蚀修复几乎只出现在无关节肿胀或肿胀改善组
标签: TEMPO研究; 依那西普; 放射学进展; 类风湿关节炎 TEMPO研究第一年影像学数据: 骨侵蚀修复几乎只出现在无关节肿胀或肿胀改善组 EULAR2007. Abstract No: OP0 ...