Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. const int leftkey=-1000000001;
  7. const int rightkey=1000000001;
  8.  
  9. struct tree{
  10. int m;
  11. int LeftChild;
  12. int RightChild;
  13. };
  14.  
  15. bool TreeorNot(vector<tree> &array, int node, int LC, int RC) {
  16. bool leftbool=true;
  17. bool rightbool=true;
  18. if (( array[node].m <= LC) || (array[node].m >= RC)) {
  19. return false;
  20. } else {
  21. if (array[node].LeftChild!=0){
  22. leftbool= TreeorNot(array, array[node].LeftChild, LC, array[node].m);
  23. }
  24. if (array[node].RightChild!=0){
  25. leftbool= TreeorNot(array, array[node].RightChild, array[node].m, RC);
  26. }
  27. return leftbool & rightbool;
  28. }
  29.  
  30.  
  31.  
  32. }
  33.  
  34.  
  35.  
  36. int main() {
  37. // ifstream cin("check.in");
  38. // ofstream cout("check.out");
  39. int n;
  40. cin >> n;
  41. vector<tree> array(n);
  42. if (n == 0) {
  43. cout << "YES";
  44. // cin.close();
  45. // cout.close();
  46. return 0;
  47. } else {
  48. for (int i = 0; i < n; ++i) {
  49. cin >> array[i].m >> array[i].LeftChild >> array[i].RightChild;
  50. --array[i].LeftChild;
  51. --array[i].RightChild;
  52. }
  53. if (TreeorNot(array, 1, leftkey, rightkey)){
  54. cout << "YES";
  55. } else {
  56. cout << "NO";
  57. }
  58. }
  59.  
  60. // cin.close();
  61. // cout.close();
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement