This repository has been archived by the owner on Aug 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
I.cpp
86 lines (66 loc) · 1.34 KB
/
I.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
struct Node
{
int val;
Node *left;
Node *right;
};
void tree_delete(Node *node)
{
if (node == NULL)
return;
if (node->left != NULL)
tree_delete(node->left);
if (node->right != NULL)
tree_delete(node->right);
free(node);
return;
}
Node *new_tree()
{
Node *ptr = (Node *)malloc(sizeof(Node));
ptr->left = NULL;
ptr->right = NULL;
ptr->val = 0;
return ptr;
}
Node *tree_add(Node *tree, int x)
{
if (tree == NULL)
tree = new_tree();
if (tree->val == x)
return tree;
if (tree->val == 0)
tree->val = x;
if (x < tree->val)
{
if (tree->left == NULL)
tree->left = new_tree();
tree_add(tree->left, x);
}
else if (x > tree->val)
{
if (tree->right == NULL)
tree->right = new_tree();
tree_add(tree->right, x);
}
return tree;
}
int AVL(Node *root)
{
if (root == NULL)
return 1;
int lh = AVL(root->left);
int rh = AVL(root->right);
return (lh == 0 || rh == 0 || abs(lh - rh) > 1) ? 0 : std::max(lh, rh) + 1;
}
int main()
{
int t;
Node *tree = new_tree();
while (std::cin >> t)
tree = tree_add(tree, t);
std::cout << (AVL(tree) == 0 ? "NO" : "YES") << std::endl;
tree_delete(tree);
return 0;
}