C++ solution using tree


#1
/*struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
      TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 };*/
 void insert(TreeNode* &root,int &n,int &f){
     if(root==NULL){
         TreeNode* t=new TreeNode(n);
         t->val=n;
         root=t;
         return;
     }
     else if(n>root->val){
         insert(root->right,n,f);
     }
     else if(n<root->val){
         if(root->right!=NULL){
             f=0;
             return;
         }
         else{
             insert(root->left,n,f);
         }
     }
 }
int Solution::solve(vector<int> &A) {
    TreeNode* root=NULL;
    for(int i=0;i<A.size();i++){
        int f=1;
        insert(root,A[i],f);
        if(f==0){
            return f;
        }
    }
    return 1;
}