Code to check if a tree is a BST
Code to check if a tree is a BST
- Code:
#include<iostream>
using namespace std;
struct node{
int data;
node *left,*right;
};
bool isBSTrec(node* p,int min,int max){
if (p==NULL) return true;
if (p->data < min || p->data > max) return false ;
return ( isBSTrec(p->left, min, p->data) && isBSTrec(p->right, p->data+1, max) );
}
bool isBST(node* p){
return isBSTrec(p,0,1000);
}
int main(){
node *root = new node;
root->data = 100;
root->left = new node;
root->left->data = 50;
root->right = new node;
root->right->data = 150;
root->left->left = new node;
root->left->left->data = 25;
root->left->right = new node;
root->left->right->data = 75;
root->left->right->left = root->left->right->left = root->right->left = root->right->right = NULL;
cout<<isBST(root);
}
ramthegreatcv- Posts : 55
Join date : 2009-01-30
Age : 36
Similar topics
» tree mirror code
» common ancestor in bst code
» Tree traversal
» Binary tree to Linked List
» longest increasing subsequence
» common ancestor in bst code
» Tree traversal
» Binary tree to Linked List
» longest increasing subsequence
Permissions in this forum:
You cannot reply to topics in this forum
|
|