[algogeeks] Re: Check if a binary tree is Binary Search Tree or not.

2012-11-14 Thread Don
Yes, you are right. How about this: bool isBST(root *t, int min=minint, int max=maxint) { return !t || ((t->value >= min) && (t->value <= max) && isBst(t->left, min, t->value) && isBst(t->right, t->value, max)); } On Nov 5, 11:04 pm, atul anand wrote: > @Don : your algo wont work f

Re: [algogeeks] Re: Check if a binary tree is Binary Search Tree or not.

2012-11-05 Thread CHIRANJEEV KUMAR
@Don, Your method fails for the case below: //Check if a binary tree is Binary Search Tree or not. #include typedef struct node { int value; struct node *left,*right; }nodeptr; nodeptr *newnode(int x) { nodeptr* tmp = (nodeptr*)malloc(sizeof(nodeptr)); tmp->value=x; tmp->left=N

Re: [algogeeks] Re: Check if a binary tree is Binary Search Tree or not.

2012-11-05 Thread atul anand
@Don : your algo wont work for following tree :- 30 / \ 20 31 / \ 9 41 above tree is not a BST bcozz here 41 should lie on the right side of the 30but it is not. so we need to keep track of max and min as we move left or right part of the tree.and each node should

Re: [algogeeks] Re: Check if a binary tree is Binary Search Tree or not.

2012-11-05 Thread shady
Understood, thanks. On Tue, Nov 6, 2012 at 2:35 AM, Don wrote: > In English, that is > > A null tree is a binary tree. > Otherwise, it's a binary tree if the root value is greater than the > left child and less than the right child, and the left and right > subtrees are binary trees. > > Don > >

[algogeeks] Re: Check if a binary tree is Binary Search Tree or not.

2012-11-05 Thread Don
In English, that is A null tree is a binary tree. Otherwise, it's a binary tree if the root value is greater than the left child and less than the right child, and the left and right subtrees are binary trees. Don On Nov 5, 2:48 pm, Don wrote: > That would work. But a simpler approach is: > > b

[algogeeks] Re: Check if a binary tree is Binary Search Tree or not.

2012-11-05 Thread Don
That would work. But a simpler approach is: bool isBinTree(root *t) { return (!t) || ((!t->left || (t->value > t->left->value)) && (!t->right || (t->value < t->right->value)) && isBinTree(t->left) && isBinTree(t->right)); } On Nov 5, 2:04 pm, shady wrote: