二叉排序树--cf675d Tree construction

给定一个数列,依次建立二叉排序树,求除根结点外,每个结点的父亲。

//二叉排序树 所有根结点都比他左儿子大,都比他右儿子小

//最后的树在地上的映射就是排好序的,所以一个点的父亲要么是排序后,在他左边的,要么事在他右边

//并且应该是两个中添加时间最晚的


#include <iostream>

#include <cstdio>

#include <set>

#include <iterator>

#include <map>

using namespace std;

const int maxn = 1e5 + 5;

int main()

{

    int n;

    cin >> n;

    set<int> st;

    map<int,int> tm;

    set<int>::iterator it1,it2;

    int tmp;

    scanf("%d",&tmp);

    int te = 0;

    st.insert(tmp);

    tm[0] = te ++;

    for (int i = 1; i < n; i ++) {

        scanf("%d",&tmp);

        it1 = st.lower_bound(tmp);

        if(it1 == st.begin()) printf("%d",*it1);

        else if(it1 == st.end()) printf("%d",*(--it1));

        else {

            it2 = it1;it1 --;

            if(tm[*it1] > tm[*it2]) printf("%d",*(it1));

            else printf("%d",*(it2));

        }

        if(i == n - 1) printf("\n");

        else printf(" ");

        st.insert(tmp);

        tm[tmp] = te ++;

    }

    return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值