forked from rudi8848/data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4.3.tree_check1.cpp
More file actions
78 lines (71 loc) · 1.35 KB
/
4.3.tree_check1.cpp
File metadata and controls
78 lines (71 loc) · 1.35 KB
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
#include <iostream>
#include <vector>
struct Node
{
long long val;
int left;
int right;
};
class Tree
{
public:
Tree(int n) {
_tree.resize(n);
int left, right;
long long val;
for (int i = 0; i < n; ++i) {
std::cin >> val >> left >> right;
_tree[i].val = val;
_tree[i].left = left;
_tree[i].right = right;
}
}
~Tree() {}
void check() {
long long full_lo, full_hi;
if (get_and_check_tree_range(0, full_lo, full_hi))
std::cout << "CORRECT" << std::endl;
else
std::cout << "INCORRECT" << std::endl;
}
private:
bool get_and_check_tree_range(int v, long long &lo, long long &hi) {
lo = hi = _tree[v].val;
if (_tree[v].left != -1)
{
long long left_lo, left_hi;
if (!get_and_check_tree_range(_tree[v].left, left_lo, left_hi))
return false;
if (! (left_lo <= left_hi))
return false;
if (left_hi >= _tree[v].val)
return false;
lo = left_lo;
}
if (_tree[v].right != -1)
{
long long right_lo, right_hi;
if (!get_and_check_tree_range(_tree[v].right, right_lo, right_hi))
return false;
if ( !(right_lo <= right_hi))
return false;
if (right_lo < _tree[v].val)
return false;
hi = right_hi;
}
return true;
}
std::vector<Node> _tree;
};
int main(void) {
int n;
std::cin >> n;
if (!n)
{
std::cout <<"CORRECT"<< std::endl;
return 0;
}
Tree tree(n);
tree.check();
return 0;
}