Raslboyy

758

Oct 3rd, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. struct Node {
  7.     Node* l;
  8.     Node* r;
  9.     int val;
  10.     Node(int val) :val(val), l(nullptr), r(nullptr){}
  11. };
  12.  
  13. Node* root = nullptr;
  14.  
  15. void add(int a, Node* cur) {
  16.     if (cur == nullptr) {
  17.         root = new Node(a);
  18.         return;
  19.     }
  20.     if (a < cur->val) {
  21.         if (cur->l == nullptr) {
  22.             cur->l = new Node(a);
  23.             return;
  24.         }
  25.         add(a, cur->l);
  26.     }
  27.     else if (a > cur->val) {
  28.         if (cur->r == nullptr) {
  29.             cur->r = new Node(a);
  30.             return;
  31.         }
  32.         add(a, cur->r);
  33.     }
  34. }
  35.  
  36. int count(Node* cur) {
  37.     if (cur == nullptr) return 0;
  38.     return count(cur->l) + count(cur->r) + 1;
  39. }
  40.  
  41. int main() {
  42.     ifstream fin("input.txt");
  43.     ofstream fout("output.txt");
  44.  
  45.     int a;
  46.     fin >> a;
  47.     while (a != 0) {
  48.         add(a, root);
  49.         fin >> a;
  50.     }
  51.     fout << count(root) << endl;
  52.  
  53.     fin.close();
  54.     fout.close();
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment