fbpx

C++: Binary Search Tree Verification

Bjarne-stroustrup
 

struct TreeNode {
	int data;
	TreeNode *left;
	TreeNode *right;
};

bool isBST(TreeNode *node, int minData, int maxData) {
	if(node == NULL) return true;
	if(node->data < minData || node->data > maxData) return false;

	return isBST(node->left, minData, node->data) && isBST(node->right, node->data, maxData);
}
//The initial call to this function can be something like this:

if(isBST(root, INT_MIN, INT_MAX)) {
	puts("This is a BST.");
} else {
	puts("This is NOT a BST!");
}

SOURCE