当前位置:网站首页>STL+树
STL+树
2022-06-24 19:28:00 【翁炜强】
"Code is beautiful and powerful that can changes the world"
--2021.8.23
题目:https://codeforces.com/problemset/problem/675/D
大意:构造一个二叉树,依次输出 输入值对应的父节点
题解:1.利用set从小到大的排序进行存储节点
2.分情况考虑
AC代码:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<set>
#include<map>
using namespace std;
const int N = 1e5;
int a[N+1];
int main()
{
a[0] = 0;
int n;
while (scanf("%d", &n) != EOF)
{
set<int>s;
scanf("%d", &a[1]);
s.insert(a[1]);
map<int, int>l, r;//代表是否有左右子树
for (int i = 2; i <= n; ++i)
{
if (i > 2) { cout << " "; }
scanf("%d", &a[i]);
set<int>:: iterator add;//获取地址
add = s.lower_bound(a[i]);//看是否能找到比它大的数
if (add == s.end())//说明没找到
{
printf("%d", *(--add));//就把它作为set中最大值的右子树
r[*(add)] = 1;//标记其为该父节点的右节点
}
else//找到比它大的数
{
if (l[*add] == 1)//如果该节点左子树已经存在
{
printf("%d", *(--add));//则把它所谓其位置-1的上个节点的右子树
r[*(add)] = 1;
}
else//若左子树存在则把它作为该节点的左子树
{
printf("%d", *(add));
l[*(add)] = 1;
}
}
s.insert(a[i]);
}
}
return 0;
}边栏推荐
- [精选] 多账号统一登录,你如何设计?
- PKI notes
- 图的邻接表存储 数组实现
- C语言实现DNS请求器
- [Web Security Basics] some details
- EditText 控制软键盘出现 搜索
- Li Kou daily question - day 25 -496 Next larger element I
- Installing Oracle without graphical interface in virtual machine centos7 (nanny level installation)
- MySQL optimizes query speed
- AntDB数据库在线培训开课啦!更灵活、更专业、更丰富
猜你喜欢

即构「畅直播」上线!提供全链路升级的一站式直播服务
![[camera Foundation (I)] working principle and overall structure of camera](/img/5d/c29d636a90d01e5c3852df2a0dd833.png)
[camera Foundation (I)] working principle and overall structure of camera

2022国际女性工程师日:戴森设计大奖彰显女性设计实力
![[camera Foundation (II)] camera driving principle and Development & v4l2 subsystem driving architecture](/img/b5/23e3aed317ca262ebd8ff4579a41a9.png)
[camera Foundation (II)] camera driving principle and Development & v4l2 subsystem driving architecture

Blender's landscape

The virtual currency evaporated $2trillion in seven months, and the "musks" ended the dream of 150000 people becoming rich

memcached全面剖析–5. memcached的应用和兼容程序

Alibaba cloud lightweight servers open designated ports

CondaValueError: The target prefix is the base prefix. Aborting.

Byte software testing basin friends, you can change jobs. Is this still the byte you are thinking about?
随机推荐
Ebpf XDP mount point analysis
2022国际女性工程师日:戴森设计大奖彰显女性设计实力
字节的软件测试盆友们你们可以跳槽了,这还是你们心心念念的字节吗?
Memcached comprehensive analysis – 3 Deletion mechanism and development direction of memcached
Multiplexer select
福建省发改委福州市营商办莅临育润大健康事业部指导视察工作
Mysql优化查询速度
【吴恩达笔记】多变量线性回归
Tournament sort
Bld3 getting started UI
自己总结的wireshark抓包技巧
SYSCALL_ Define5 setsockopt code flow
关于Unity中的transform.InverseTransformPoint, transform.InverseTransofrmDirection
leetcode_191_2021-10-15
Graduation summary of phase 6 of the construction practice camp
传输层 udp && tcp
LeetCode-513. 找树左下角的值
01---两列波在相遇处发生干涉的条件
Installing Oracle without graphical interface in virtual machine centos7 (nanny level installation)
Arkit与Character Creator动画曲线的对接