他究竟是爱我,还是拿我当炮友# Love - 情爱幽幽
i*9
1 楼
很多人给出的答案
int isBST(struct node* node) {
if (node==NULL) return(true);
// false if the max of the left is > than us
// (bug -- an earlier version had min/max backwards here)
if (node->left!=NULL && maxValue(node->left) > node->data)
return(false);
// false if the min of the right is <= than us
if (node->right!=NULL && minValue(node->right) <= node->data)
return(false);
// false if, recursively, the left or right is not a BST
if (!isBST(node->left) || !isBST(node->right))
return(false);
// passing all that, it's a BST
return(true);
}
我怎么觉得在第三次判断的时候应该是(只有小于号,没有等于号)
// false if the min of the right is < than us
if (node->right!=NULL && minValue(node->right) < node->data)
return(false);
右子树值等于root值也有应该是valid binary search tree, 大家讨论讨论?
int isBST(struct node* node) {
if (node==NULL) return(true);
// false if the max of the left is > than us
// (bug -- an earlier version had min/max backwards here)
if (node->left!=NULL && maxValue(node->left) > node->data)
return(false);
// false if the min of the right is <= than us
if (node->right!=NULL && minValue(node->right) <= node->data)
return(false);
// false if, recursively, the left or right is not a BST
if (!isBST(node->left) || !isBST(node->right))
return(false);
// passing all that, it's a BST
return(true);
}
我怎么觉得在第三次判断的时候应该是(只有小于号,没有等于号)
// false if the min of the right is < than us
if (node->right!=NULL && minValue(node->right) < node->data)
return(false);
右子树值等于root值也有应该是valid binary search tree, 大家讨论讨论?