【发布时间】: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