0
我正在嘗試連接處於同一級別的二叉樹的節點 我正在使用隊列來執行此操作。最初我已經將根推入隊列。但在第12行時,如果(q.front() - > left!= NULL)執行的第一個if語句是 ,那麼我得到一個運行時錯誤。 請幫助通過隊列訪問節點時出現運行時錯誤
節點結構:
struct node
{
int data;
struct node* left;
struct node* right;
struct node* nextRight;
};
功能進行連接,則需要作很大的改動在你的代碼中的節點
node* connect(node* root)
{
if(root=NULL)
return display(root);
queue<node*> q;
int count=0;
q.push(root);
count++;
while(!q.empty())
{
cout << "inside while loop\n";
int i=1,temp=0;
while(i<=count)
{
cout << "inside inner while\n";
if(q.front()->left!=NULL)
{
cout << "inside if 1\n";
q.push(q.front()->left);
temp++;
}
if(q.front()->right!=NULL)
{
cout << "inside if 2\n";
q.push(q.front()->right);
temp++;
}
node* v=q.front();
q.pop();
if(i==count)
v->nextRight=NULL;
else
v->nextRight=q.front();
}
count=temp;
}
return display(root);
}
什麼是運行時錯誤? –
分段錯誤 – Rahul