Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. bool isBST(int k,int tree[200000][3], int min, int max){
  2.     if (k==0) return true;
  3.     int lin=tree[k][1];
  4.     int rin=tree[k][2];
  5.     cout << tree[k][0]<<" ";
  6.     if(tree[k][0]<min || tree[k][0]>max) return false;
  7.  
  8.     return isBST(lin,tree,min,tree[k][0] ) && isBST(rin,tree,tree[k][0],max );
  9. }
  10.  
  11. bool isBST(int k,int tree[200000][3]){
  12.     return isBST(k,tree, INT_MIN,INT_MAX);
  13.  
  14. }
  15.  
  16. int main() {
  17.  
  18.     ifstream fin("check.in");
  19.     ofstream fout("check.out");
  20.     int n;
  21.     int temp;
  22.     int height = 0;
  23.     fin >> n;
  24.     int tree[200000][3];
  25.  
  26.     for (int i = 1; i <=n; i++) {
  27.         fin >> temp;
  28.         tree[i][0] = temp;
  29.         fin >> temp;
  30.         tree[i][1] = temp;
  31.         fin >> temp;
  32.         tree[i][2] = temp;
  33.     }
  34.     int h;
  35.     string str;
  36.     if(n>0){
  37.         (isBST(1,tree)) ? str="YES" : str="NO";
  38.         fout << str;
  39.     }
  40.     else fout << "YES";
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement