【问题标题】:Is this a binary search tree ? HackerRank Problem这是二叉搜索树吗? HackerRank 问题
【发布时间】:2020-01-02 16:57:50
【问题描述】:

我试图通过使用 Tree: level order traversal 来解决这个问题是来自 HackerRank 的二叉搜索树。但它显示编译错误。是因为使用queue吗?隐藏的存根代码将在此问题中传递一个参数。问题链接:https://www.hackerrank.com/challenges/is-binary-search-tree/problem。这是我的代码:

bool checkBST(Node* root) {
        if(root == NULL){
            return true;
        }

        queue<Node*> Q;
        Q.push(root);
        while(!Q.empty()){
            Node *current_node = Q.front();

            if(current_node->left != NULL){
                if(current_node->left->data >= current_node->data){
                    return false;
                }
                Q.push(current_node->left);
            }
            if(current_node->right != NULL){
                if(current_node->right->data <= current_node->data){
                    return false;
                }
                Q.push(current_node->right);
            }

            Q.pop();
        }
      return true;
    }

【问题讨论】:

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


    【解决方案1】:

    刚刚看了前面提到的hackerrank挑战,是的,错误是你没有包含#include &lt;queue&gt;但是你不能在这个挑战中添加头文件。你必须用递归来做这个问题。

    如果您想获得有关如何编写递归的提示,请随时询问。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多