#include<iostream> #include<algorithm> using namespace std; struct TreeNode { int data; TreeNode *parent,*left,*right; }; TreeNode *pTree; int main() { int n,p; cin>>n; for(int i=0; i<n; i++) { cin>>p; arr[i]=p; if(pTree == NULL) { pTree=new TreeNode; pTree->data=p; pTree->left=pTree->right=NULL; } else { TreeNode *point = pTree; while(true) { if(point->data > p) { if(point->left==NULL) { point->left = new TreeNode; point->left->data=p; point->left->left=point->left->right=NULL; break; } else { point=point->left; continue; } } if(point->data <= p) { if(point->right==NULL) { point->right = new TreeNode; point->right->data=p; point->right->left=point->right->right=NULL; break; } else { point=point->right; continue; } } } } } return 0; }
4 total views, 4 views today