2015-04-15 73 views
0

爲什麼我的下面的代碼進入無限循環?我查了一下refernce- Level Order Traversal of a Binary Tree,我沒有發現這個和我的代碼有太大的區別。那麼究竟是什麼問題呢?等級順序在BST中遍歷

void levelorder(struct node *root) 
{ 
queue<struct node*> q; 
q.push(root); 
while (!q.empty()){ 

    const node * const temp = q.front(); 
    q.pop(); 
    cout<<temp->value << " "; 
    if(root->left) 
     q.push(root->left); 
    if(root->right) 
     q.push(root->right); 
    } 
} 

回答

0

級順序遍歷:

輸入8,4,2,3,12

我的輸出8 4 2 12 3在該一個(錯誤輸出)

是從0到3(樹的高度)

int level(tree *start, int a) 
    { 
     if(start==NULL) 
     return(-1); 
     if(a==0) 
     printf("%d ",start->info); 
     else 
     { 
     level(start->left,--a); 
     level(start->right,--a); 
     } 
    }