#include <iostream> #include <ratio> using namespace std; struct TreeNode { int data; TreeNode* leftchild; TreeNode* rightchild; TreeNode(int x):data(x),leftchild(NULL),rightchild(NULL){} }; TreeNode* Insert(TreeNode* root,int x,int father) { if(root==NULL) { root=new TreeNode(x);//每次插入成功后, cout<<father<<endl;//求相应的父亲节点的关键字值 } else if(x<root->data) { root->leftchild=Insert(root->leftchild,x,root->data); } else { root->rightchild=Insert(root->rightchild,x,root->data); } return root; } int main() { int n; while (cin >> n) { TreeNode* root=NULL;// for(int i=1;i<=n;i++) { int x; cin>>x; root=Insert(root,x,-1);//不断更新root节点 } } }