Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- struct Node {
- int value;
- int left;
- int right;
- };
- Node Arr[200001];
- int minimum = -1e9 - 1;
- int maximum = 1e9 + 1;
- bool check(int strNum, int maximum, int minimum)
- {
- if(Arr[strNum].value > maximum || Arr[strNum].value < minimum) return false;
- if(Arr[strNum].left != 0 && !(check(Arr[strNum].left, Arr[strNum].value - 1, minimum)))
- {
- return false;
- }
- if(Arr[strNum].right != 0 && !(check(Arr[strNum].right, maximum, Arr[strNum].value + 1)))
- {
- return false;
- }
- return true;
- }
- int main()
- {
- freopen("check.in", "r", stdin);
- freopen("check.out", "w", stdout);
- int n;
- cin >> n;
- if(!n)
- {
- cout << "YES";
- return 0;
- }
- int key1, key2, key3;
- for(int i = 1; i < n + 1; i++) {
- cin >> key1;
- Arr[i].value = key1;
- cin >> key2;
- Arr[i].left = key2;
- cin >> key3;
- Arr[i].right = key3;
- }
- if(check(1, maximum, minimum)) cout << "YES";
- else cout << "NO";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement