Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. struct Node {
  7.     int value;
  8.     int left;
  9.     int right;
  10. };
  11.  
  12. Node Arr[200001];
  13. int minimum = -1e9 - 1;
  14. int maximum = 1e9 + 1;
  15.  
  16. bool check(int strNum, int maximum, int minimum)
  17. {
  18.     if(Arr[strNum].value > maximum || Arr[strNum].value < minimum) return false;
  19.     if(Arr[strNum].left != 0 && !(check(Arr[strNum].left, Arr[strNum].value - 1, minimum)))
  20.     {
  21.         return false;
  22.     }
  23.     if(Arr[strNum].right != 0 && !(check(Arr[strNum].right, maximum, Arr[strNum].value + 1)))
  24.     {
  25.         return false;
  26.     }
  27.     return true;
  28. }
  29.  
  30. int main()
  31. {
  32.     freopen("check.in", "r", stdin);
  33.     freopen("check.out", "w", stdout);
  34.     int n;
  35.     cin >> n;
  36.     if(!n)
  37.     {
  38.         cout << "YES";
  39.         return 0;
  40.     }
  41.     int key1, key2, key3;
  42.     for(int i = 1; i < n + 1; i++) {
  43.         cin >> key1;
  44.         Arr[i].value = key1;
  45.         cin >> key2;
  46.         Arr[i].left = key2;
  47.         cin >> key3;
  48.         Arr[i].right = key3;
  49.     }
  50.     if(check(1, maximum, minimum)) cout << "YES";
  51.     else cout << "NO";
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement