@gaurav yes this code takes care of left and right child if misplaced.if you want to check if tree is correctly formed then you will have to keep track of min and max value.agreed..:)
You are given n real numbers in an array. A number in the array is called a decimal dominant if it occurs more than n/10 times in the array. Give an O(n) time algorithm to determine if the given array has a decimal dominant.
Implement a function getbits, that returns the(right adjusted) n bits that begin at position p of an integer. Assume bit position 0 is at the right end and that n and p are sensible positive values.
int isBST(struct node *root)
ReplyDelete{
if(root == NULL) return 1;
else if(root->left->value > root->value || root->right->value < root->value) return 0;
else return(isBST(root->left) && isBST(root->right))
}
Its wrong....
ReplyDeleteCounter test case :
5
/ \
2 6
/ \
1 9
Its not BST... ur code will return true.
U will have to keep a max and min of a subtree too.
@gaurav yes this code takes care of left and right child if misplaced.if you want to check if tree is correctly formed then you will have to keep track of min and max value.agreed..:)
ReplyDeleteint isBST(struct node* node, int min, int max)
ReplyDelete{
/* an empty tree is BST */
if (node==NULL)
return 1;
/* false if this node violates the min/max constraint */
if (node->data < min || node->data > max)
return 0;
return
isBST(node->left, min, node->data) &&
isBST(node->right, node->data+1, max);
}
call isBST(node,INT_MIN, INT_MAX) initially.