Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- struct node
- {
- int key;
- node *l, *r;
- };
- int in(node* root, node* el, int h)
- {
- if (root->l && root->key > el->key)
- return in(root->l, el, h) + 1;
- if (root->r && root->key < el->key)
- return in(root->r, el, h) + 1;
- if (root->key > el->key)
- {
- root->l = el;
- return h + 1;
- }
- if (root->key < el->key)
- {
- root->r = el;
- return h + 1;
- }
- }
- int main()
- {
- // freopen("A.in", "r", stdin);
- int f;
- cin >> f;
- if (f == 0)
- cout << 0;
- else
- {
- node *root = new node;
- root->l = root->r = nullptr;
- root->key = f;
- int ans = 0;
- while(cin)
- {
- int curr;
- cin >> curr;
- if (curr == 0)
- break;
- node *el = new node;
- el->l = el->r = nullptr;
- el->key = curr;
- int h = 0;
- ans = max(in(root, el, 1), ans);
- }
- cout << ans;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment