给定一个数列,依次建立二叉排序树,求除根结点外,每个结点的父亲。
//二叉排序树 所有根结点都比他左儿子大,都比他右儿子小
//最后的树在地上的映射就是排好序的,所以一个点的父亲要么是排序后,在他左边的,要么事在他右边
//并且应该是两个中添加时间最晚的
#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;
}