Advertisement
Guest User

Untitled

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