Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- class Node{
- public:
- int data;
- Node *left,*right;
- Node(int key){
- data=key;
- left=right=NULL;
- }
- };
- int Check(Node * root,int minimum,int maximum){
- if(root==NULL)
- return 1;
- if(minimum>root->data&& maximum<root->data)
- return 0;
- return Check(root->left,minimum,root->data-1) && Check(root->right,root->data+1,maximum);
- }
- int CheckBST(Node* root){
- if(root==NULL)
- return 1;
- return Check(root,INT_MIN,INT_MAX);
- }
- void Insert(Node **root_ref,int key){
- if(*root_ref==NULL){
- *root_ref=new Node(key);
- return;
- }
- if((*root_ref)->data<key)
- Insert(&((*root_ref)->right),key);
- else
- if((*root_ref)->data>key)
- Insert(&((*root_ref)->left),key);
- }
- int main(){
- Node *root=NULL;
- Insert(&root,5);
- Insert(&root,7);
- Insert(&root,2);
- Insert(&root,8);
- if(CheckBST(root))
- cout<<" yes it is a BST ";
- else
- cout<<" no ";
- }
Add Comment
Please, Sign In to add comment