構造体があり、この構造体のキューを作成したいと考えています。新しい要素のプッシュに問題があります。助けてください。
#include <iostream>
#include <queue>
#include <deque>
#include <list>
using namespace std;
struct Node {
Node *left, *right;
int key;
};
queue<Node> q;
void updateLevel( Node &n, int &level){
if(n.left!=NULL && n.right!=NULL){
level+=2;
q.push(n.left);
q.push(n.right);
}
else if(n.left!=NULL || n.right!=NULL){
level++;
if(n.left!=NULL) q.push(n.left);
if(n.right!=NULL) q.push(n.right);
}
};
void printTree(Node root){
//if(root!=NULL){
q.push(root);
Node n;
while(!q.empty()){
n =q.pop();
updateLevel(n,nextLevel);
curLevel--;
cout<<n.key;
if(curLevel<=0){
cout<<endl;
curLevel=nextLevel;
nextLevel=0;
}
}
//}
};
int main() {
Node rootTree;
printTree(rootTree);
return 0;
}
この関数をメインから呼び出しています。NULL に関する if 条件のチェックインでもエラーが発生しました。助けてください