判断二叉树是否为二叉搜索树BST

一、

/* Returns true if the given tree is a binary search tree
 (efficient version). */
int isBST(struct node* node)
{
    return(isBSTUtil(node, INT_MIN, INT_MAX));
}

/* Returns true if the given tree is a BST and its
   values are >= min and <= max. */
int isBSTUtil(struct node* node, int min, int max)
{

    /* an empty tree is BST */
    if (node==NULL)
        return 1;

    /* false if this node violates the min/max constraint */
    if (node->data < min || node->data > max)
        return 0;

    /* otherwise check the subtrees recursively,
     tightening the min or max constraint */
    return
        isBSTUtil(node->left, min, node->data-1) &&  // Allow only distinct values
        isBSTUtil(node->right, node->data+1, max);  // Allow only distinct values
}

二、中序遍历

1) Do In-Order Traversal of the given tree and store the result in a temp array.
2) Check if the temp array is sorted in ascending order, if it is, then the tree is BST.

Time Complexity: O(n)

We can avoid the use of Auxiliary Array. While doing In-Order traversal, we can keep track of previously visited node. If the value of the currently visited node is less than the previous value, then tree is not BST. 

bool isBST(struct node* root)
{
    static struct node *prev = NULL;  //static只初始化一次!!!
     
    // traverse the tree in inorder fashion and keep track of prev node
    if (root)
    {
        if (!isBST(root->left))
          return false;
 
        // Allows only distinct valued nodes 
        if (prev != NULL && root->data <= prev->data)
          return false;
 
        prev = root;
 
        return isBST(root->right);
    }
 
    return true;
}
参考: http://cslibrary.stanford.edu/110/BinaryTrees.html


判断一个顺序存储的二叉树是否二叉搜索树Binary Search TreeBST),可以采用递归的方式。首先定义两个辅助函数,`isBSTHelper`用于递归检查当前节点是否满足BST的性质以及左右子树都是BST,另一个函数`isBST`作为入口。 以下是C语言的示例算法: ```c #include <stdio.h> // 定义二叉树节点结构 typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; // 辅助函数,用于递归检查BST性质 int isBSTHelper(TreeNode* node, int minVal, int maxVal) { if (!node) return 1; // 空节点视为满足BST if (node->val <= minVal || node->val > maxVal) { return 0; // 越界,不是BST } // 递归检查左、右子树 return isBSTHelper(node->left, minVal, node->val - 1) && isBSTHelper(node->right, node->val + 1, maxVal); } // 主函数,判断顺序存储的二叉树是否BST int isBST(TreeNode* root) { return isBSTHelper(root, INT_MIN, INT_MAX); } int main() { // 测试代码(这里只提供一个简化的例子) TreeNode* root = createTreeNode(4); // 假设已有一个创建好的根节点 // ... 先设置好二叉树结构 ... if (isBST(root)) { printf("The binary tree is a BST.\n"); } else { printf("The binary tree is not a BST.\n"); } return 0; } ``` 在这个算法中,`isBSTHelper`函数会从根节点开始,如果当前节点的值在其范围(minVal到maxVal)内并且其子树也满足BST条件,那么返回1表示当前节点是BST的一部分;反之,返回0表示不是BST。主函数`isBST`通过传入整数最小值和最大值(这里是负无穷和正无穷)来验证整个树是否BST
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值