#include <iostream>

using namespace std;

struct TNode {
  int m_nVal;
  TNode *m_pLeft;
  TNode *m_pRight;
};

int getMin(TNode *pTree) {
  int min = pTree->m_nVal;

  if(pTree->m_pLeft!=NULL) {
    int min1 = getMin(pTree->m_pLeft);

    if(min1 < min)
      min = min1;
  }

  if(pTree->m_pRight!=NULL) {
    int min2 = getMin(pTree->m_pRight);

    if(min2 < min)
      min = min2;
  }

  return min;
}

int getMax(TNode *pTree) {
  int max = pTree->m_nVal;

  if(pTree->m_pLeft!=NULL) {
    int max1 = getMax(pTree->m_pLeft);

    if(max1 > max)
      max = max1;
  }

  if(pTree->m_pRight!=NULL) {
    int max2 = getMax(pTree->m_pRight);

    if(max2 > max)
      max = max2;
  }

  return max;
}

bool isBST(TNode *pTree) {
  if(pTree==NULL)
    return true;

  if(pTree->m_pLeft!=NULL) {
    int maxL = getMax(pTree->m_pLeft);

    if(maxL > pTree->m_nVal)
      return false;
  }

  if(pTree->m_pRight!=NULL) {
    int minR = getMin(pTree->m_pRight);

    if(minR < pTree->m_nVal)
      return false;
  }

  return isBST(pTree->m_pLeft) && isBST(pTree->m_pRight);
}

int main(int argc, char **argv) {

  return 0;
}
