#include<cstdio> #include<string> struct TreeNode { int data; TreeNode* LChild; TreeNode* RChild; }; void InsertBST(TreeNode*& root, int data) { TreeNode* pa = new TreeNode; pa->data = data; pa->LChild = NULL; pa->RChild = NULL; if (root == NULL) { root = pa; printf("-1\n"); } else { TreeNode* ppre = root; TreeNode* pcur; while (true) { if (data < ppre->data) { pcur = ppre->LChild; if (pcur == NULL) { ppre ->LChild = pa; printf("%d\n", ppre->data); break; } else { ppre = pcur; } } else { pcur = ppre->RChild; if (pcur == NULL) { ppre->RChild = pa; printf("%d\n", ppre->data); break; } else { ppre = pcur; } } } } } int main() { TreeNode* root = NULL; int n ; scanf("%d", &n); for (int i = 0 ; i < n; ++i) { int arr; scanf("%d", &arr); InsertBST(root, arr); } }