2016-08-03 89 views
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); 
} 
+0

什麼是運行時錯誤? –

+0

分段錯誤 – Rahul

回答

0

錯誤1:

while(i<=count) 

永遠不會爲真。

錯誤2:

v->nextRight=q.front(); 

將設置nextRight指向左子。

錯誤3:

i永遠不會改變,那爲什麼聲明一個變量,製造混亂。

+0

謝謝我已經做出了改變,現在它正在工作 – Rahul

相關問題