LeetCode 98. Validate Binary Search Tree

代码:

class Solution 
{
public:
    bool isValidBST(TreeNode *root) 
    {
    	return gao(root, INT_MIN, INT_MAX);
    }

private:
	bool gao(TreeNode *node, int lower_bound, int upper_bound)
	{
		if (node==NULL || (node->left==NULL && node->right==NULL))
		{
			return true;
		} else if (node->left!=NULL && node->right!=NULL)
		{
			return node->left->val < node->val
				&& node->left->val > lower_bound
				&& node->right->val > node->val
				&& node->right->val < upper_bound
				&& gao(node->left, lower_bound, node->val)
				&& gao(node->right, node->val, upper_bound);
		} else if (node->left==NULL && node->right!=NULL)
		{
			return node->right->val > node->val
				&& node->right->val < upper_bound
				&& gao(node->right, node->val, upper_bound);
		} else if (node->left!=NULL && node->right==NULL)
		{
			return node->left->val < node->val
				&& node->left->val > lower_bound
				&& gao(node->left, lower_bound, node->val);
		}
	}
};


你可能感兴趣的:(LeetCode,C++)