【问题标题】:Is given binary tree is binary search tree or not给定二叉树是否为二叉搜索树
【发布时间】:2017-09-28 18:54:47
【问题描述】:

我写了一个函数,如果给定的二叉树是二叉搜索树,则返回 true,否则返回 false。

bool IsBst(node* root)
{
    if(root->left == NULL && root->right == NULL)
    {
        return true;
    }
    if(root->left->data <= root->data && root->right->data > root->data)
    {
        return (IsBst(root->left) && IsBst(root->right))
    }
    else
    {
        else false;
    }
}

我的功能对吗?

这个函数会返回正确的答案吗?

我怀疑如果左孩子为空,那么这个比较root-&gt;left-&gt;data&lt;=root-&gt;data会返回什么?(如果有空)

帮助我改进这一点! 提前致谢!

【问题讨论】:

    标签: c++ algorithm tree binary-search-tree


    【解决方案1】:

    应该是这样的

    bool IsBst(const node* root, node* minNode = nullptr, node* maxNode = nullptr)
    {
        if (root == nullptr) {
            return true;
        }
        if (minNode != nullptr && root->data < minNode->data)
        {
            return false;
        }
        if (maxNode != nullptr && maxNode->data < root->data)
        {
            return false;
        }
        return IsBst(root->left, minNode, root)
            && IsBst(root->right, root, maxNode);
    }
    

    【讨论】:

      【解决方案2】:

      如果您使用的是 C++ 17 及更高版本,则可以使用 optional 类更优雅地完成此操作。因此,您不需要对minmax 进行nullptr 检查:

      bool checkBST0(const Node* root, const std::optional<int>& min, const std::optional<int>& max) {
          if (root != nullptr) {
              const auto data = root->data;
              if ((min.has_value() && min >= data) || 
                  (max.has_value() && max <= data)) {
                  return false;
              }
                  
              std::optional<int> opt(data);
              return checkBST0(root->left, min, opt) && checkBST0(root->right, opt, max);
          }
              
          return true;
      } 
      

      最初,您应该使用不带任何值的optional 调用此方法:

      std::optional<int> emptyOptional;
      return checkBST0(root, emptyOptional, emptyOptional);  
      

      【讨论】:

        【解决方案3】:

        不,这是不对的。它会在这棵树上失败:

             3
              \
               \
                5
        

        它会在这个问题上给出错误的答案:

              4
             / \
            /   \
           /     \
          2       6
         / \     / \
        1   9   0   8
        

        BST 被定义为一棵树,其每个内部节点都存储一个大于该节点左子树中所有键且小于其右子树中所有键的键(参见Wikipedia article)。

        因此,对于我的示例中的 1-2-9 左子树来说,左节点值小于它的根 (12) 是不够的。它还应满足其所有节点的值都小于整个树根中的值 4 的条件。

        这是我在问题Pseudo code to check if binary tree is a binary search tree - not sure about the recursion的答案中给出的一个例子:

        // Test a node against its closest left-side and right-side ancestors
        boolean isNodeBST(NODE *lt, NODE *node, NODE *rt)
        {
            if(node == NULL)
                return true;
            if(lt != NULL && node->key < lt->key)
                return false;
            if(rt != NULL && node->key > rt->key)
                return false;
        
            return
                isNodeBST(lt, node->left, node) &&
                isNodeBST(node, node->right, rt);
        }
        
        boolean isTreeBST(TREE *tree)
        {
           return isNodeBST( NULL, tree->root, NULL);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-06-05
          • 1970-01-01
          • 2023-03-08
          • 1970-01-01
          • 2019-04-09
          • 2021-10-01
          • 1970-01-01
          相关资源
          最近更新 更多