Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- using namespace std;
- struct Node {
- long long key;
- Node * left;
- Node * right;
- Node(long long _key) {
- key = _key;
- left = nullptr;
- right = nullptr;
- }
- };
- void insert(Node * root, long long key) {
- if (key < root->key) {
- if (root->left == nullptr) {
- root->left = new Node(key);
- }
- insert(root->left, key);
- return;
- }
- if (key > root->key) {
- if (root->right == nullptr) {
- root->right = new Node(key);
- }
- insert(root->right, key);
- return;
- }
- }
- int height(Node * root) {
- if (root == nullptr) {
- return 0;
- }
- return max(height(root->left), height(root->right)) + 1;
- }
- int main() {
- long long num;
- cin >> num;
- Node * root = new Node(num);
- while (cin >> num) {
- if (num == 0) {
- break;
- }
- insert(root, num);
- }
- cout << height(root);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement