The_Law

Untitled

May 10th, 2017
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct node
  6. {
  7.     int key;
  8.     node *l, *r;
  9. };
  10.  
  11. int in(node* root, node* el, int h)
  12. {
  13.     if (root->l && root->key > el->key)
  14.         return in(root->l, el, h) + 1;
  15.     if (root->r && root->key < el->key)
  16.         return in(root->r, el, h) + 1;
  17.     if (root->key > el->key)
  18.     {
  19.         root->l = el;
  20.         return h + 1;
  21.     }
  22.     if (root->key < el->key)
  23.     {
  24.         root->r = el;
  25.         return h + 1;
  26.     }
  27. }
  28.  
  29. int main()
  30. {
  31. //    freopen("A.in", "r", stdin);
  32.  
  33.     int f;
  34.     cin >> f;
  35.  
  36.     if (f == 0)
  37.         cout << 0;
  38.     else
  39.     {
  40.         node *root = new node;
  41.         root->l = root->r = nullptr;
  42.         root->key = f;
  43.  
  44.         int ans = 0;
  45.  
  46.         while(cin)
  47.         {
  48.             int curr;
  49.             cin >> curr;
  50.  
  51.             if (curr == 0)
  52.                 break;
  53.  
  54.             node *el = new node;
  55.             el->l = el->r = nullptr;
  56.             el->key = curr;
  57.  
  58.             int h = 0;
  59.             ans = max(in(root, el, 1), ans);
  60.         }
  61.  
  62.         cout << ans;
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment