2015-04-15 71 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); 
     } 
    }