#include <iostream>
using namespace std;
struct node {
int val;
node *l, *r;
node(int x) : val(x), l(nullptr), r(nullptr) {}
};
int insertTree(node *&root, int x) {
if (root == nullptr) {
root = new node(x);
return -1;
}
int num = 0;
if (x <= root->val) {
num = insertTree(root->l, x);
} else {
num = insertTree(root->r, x);
}
if (num == -1) return root->val;
return num;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int n;
cin >> n;
node *root = nullptr;
while (n--) {
int x;
cin >> x;
int ans = insertTree(root, x);
cout << ans << endl;
}
return 0;
}
|