验证二叉树是否是BST(C语言实现)
int isBST(BTNode *node, int min, int max)
函数中包括两个参数min, max,主函数中call时用 isBST(node, -10000, 10000)
解题思路
前序遍历
从根结点开始向下走,只要向左走,结点一定在变小(左子树<根); 只要向右走,结点一定在变大(右子树>根)
int isBST(BTNode *node, int min, int max) // the item stored in node has to be smaller than max and larger than min
{
// write your code here
if(node == NULL) return 1;
if(node->item <= min || node->item >= max) return 0;
return isBST(node->left, min, node->item) && isBST(node->right, node->item, max);
}