当前位置:网站首页>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;
}边栏推荐
- What does CTO (technical director) usually do?
- TCP_ Nodelay and TCP_ CORK
- C语言-关键字1
- 基于ASP.NET开发的固定资产管理系统源码 企业固定资产管理系统源码
- About transform InverseTransformPoint, transform. InverseTransofrmDirection
- Functional analysis of ebpf tracepoint
- When to send the update windows message
- HCIA assessment
- 力扣每日一题-第26天-496.下一个更大元素Ⅰ
- Blender's simple skills - array, rotation, array and curve
猜你喜欢

Dynamic routing protocol rip, OSPF

C语言实现DNS请求器

Multi view function in blender

Introduce the overall process of bootloader, PM, kernel and system startup

架构实战营 第 6 期 毕业设计

SAP接口debug设置外部断点

Auto. JS to automatically authorize screen capture permission

Sslhandshakeexception: no subject alternative names present - sslhandshakeexception: no subject alternative names present

去掉录屏提醒(七牛云demo)

C语言-关键字1
随机推荐
Understanding openstack network
leetcode1863_2021-10-14
福建省发改委福州市营商办莅临育润大健康事业部指导视察工作
【产品设计研发协作工具】上海道宁为您提供蓝湖介绍、下载、试用、教程
Sslhandshakeexception: no subject alternative names present - sslhandshakeexception: no subject alternative names present
Transport layer UDP & TCP
Please open online PDF carefully
Arkit与Character Creator动画曲线的对接
EasyBypass
虚拟机CentOS7中无图形界面安装Oracle(保姆级安装)
Why are life science enterprises on the cloud in succession?
Debugging Analysis of Kernel panics and Kernel oopses using System Map
手动事务的几个类
Shengzhe technology AI intelligent drowning prevention service launched
使用Adb连接设备时提示设备无权限
123. the best time to buy and sell shares III
XTransfer技术新人进阶秘诀:不可错过的宝藏Mentor
架构实战营 第 6 期 毕业总结
力扣每日一题-第26天-496.下一个更大元素Ⅰ
Docking of arkit and character creator animation curves