2. Check BST 2 #

Created Friday 07 February 2020

**Bottom Up Approach - we need to return multiple things, recursion only takes us down. **

pair<pair<int, int>, bool> isBST(BT*)
{
	pair<pair<int, int>, bool> ret;
	ret.first.first = INT_MIN;
	ret.first.second = INT_MAX;
	ret.second = false;
	if(root==NULL)
	{
		ret.second = true;
		return ret;
	}
	if(root->left==NULL && root->right==NULL)
	{
		ret.first.first = root->data;	// minimum and maximum are the same
		ret.first.second = root->data;
		ret.second = true;
		return ret;
	}
	if(root->left!=NULL)
		ret = isBST(root->left);
	if(ret.second) // we are only worried about the minimum
	{
		pair<pair<int, int>, bool> ret2;
			ret2 = isBST(root->right);
			ret.first.second = ret2.second.second; // maximum
			ret.second &&= re2.second;
			return ret;
	}
}

Time complexity: T(n) = 2T(n/2) + k, O(n) is independent of height.