Thursday, Aug 16th

Last update12:59:40 PM GMT

Write C code to check if a given binary tree is a binary search tree or not.

Write e-mail

A binary tree is a binary search tree(BST) if for any node's value, the left node has a smaller value and right node has a larger value. So, again if we solve this in a recursive manner starting from root and making recursive calls on left and right nodes.

argaiv1770


	int isThisABST(struct node* mynode) 

	{ 

	if (mynode==NULL) return(true); 

	if (node->left!=NULL && maxValue(mynode->left) > mynode->data) 

	return(false);

	if (node->right!=NULL && minValue(mynode->right) <= mynode->data) 

	return(false); 

	if (!isThisABST(node->left) || !isThisABST(node->right)) 

	return(false); 

	

	return(true); 

	}

	

Share this post



Web Hosting