The_Law

Untitled

May 10th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Tree
  6. {
  7.     int num;
  8.     Tree* Left;
  9.     Tree* Right;
  10.     Tree(int x) : num(x), Left(NULL), Right(NULL) { }
  11. };
  12.  
  13. int add(Tree* &t, int num)
  14. {
  15.     if (!t)
  16.     {
  17.         t = new Tree(num);
  18.         return 1;
  19.     }
  20.     if (t->num == num) return 0;
  21.     if (t->num > num) return add(t->Left, num);
  22.     return add(t->Right, num);
  23. }
  24.  
  25. int main()
  26. {
  27.     Tree* tr = NULL;
  28.     int ans = 0;
  29.     int n;
  30.     cin >> n;
  31.     while (n)
  32.     {
  33.         ans += add(tr, n);
  34.         cin >> n;
  35.     }
  36.     cout << ans << endl;
  37.     return 0;
  38. }
Add Comment
Please, Sign In to add comment