当前位置:网站首页>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;
}边栏推荐
- Based on asp Net development of fixed assets management system source code enterprise fixed assets management system source code
- leetcode1863_2021-10-14
- Data link layer & some other protocols or technologies
- Memcached comprehensive analysis – 2 Understand memcached memory storage
- SAP接口debug设置外部断点
- EasyBypass
- socket(2)
- Fuzhou business office of Fujian development and Reform Commission visited the health department of Yurun university to guide and inspect the work
- C语言-关键字1
- Pattern recognition - 1 Bayesian decision theory_ P1
猜你喜欢

VirtualBox virtual machine installation win10 Enterprise Edition

【产品设计研发协作工具】上海道宁为您提供蓝湖介绍、下载、试用、教程

Make tea and talk about heroes! Leaders of Fujian Provincial Development and Reform Commission and Fujian municipal business office visited Yurun Health Division for exchange and guidance

Vscode netless environment rapid migration development environment (VIP collection version)

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

Big factories go out to sea and lose "posture"

VSCode无网环境快速迁移开发环境(VIP典藏版)

【吴恩达笔记】多变量线性回归

memcached全面剖析–2. 理解memcached的內存存儲

架构实战营 第 6 期 毕业设计
随机推荐
Please open online PDF carefully
Transport layer UDP & TCP
网络层 && IP
Mysql优化查询速度
【论】A deep-learning model for urban traffic flow prediction with traffic events mined from twitter
字节的软件测试盆友们你们可以跳槽了,这还是你们心心念念的字节吗?
Network layer & IP
应用实践 | 海量数据,秒级分析!Flink+Doris 构建实时数仓方案
Graduation summary of phase 6 of the construction practice camp
装修首页自定义全屏视频播放效果gif动态图片制作视频教程播放代码操作设置全屏居中阿里巴巴国际站
多路转接select
手动事务的几个类
一文理解OpenStack网络
66 pitfalls in go programming language: pitfalls and common errors of golang developers
Blender's simple skills - array, rotation, array and curve
LeetCode-513. 找树左下角的值
Apple mobile phone can see some fun ways to install IPA package
RFC 793 why to send reset and when to send reset
Volcano成Spark默认batch调度器
Static routing experiment