2014-02-12 222 views
1

我不知道如何將以下代碼遍歷樹:樹的遍歷,遞歸

//pre order travel 
void travel (BST *tree) 
{ 
    the 
    if(tree!=NULL) 
    { 
     printf("%d ",tree->info); 
     travel(tree->left); 
     travel(tree->right); 
    } 
} 

我收到與travel(tree->left)travel(tree->right)遞歸調用混淆。 如果我有:

 A 
    B  C 
    D  E F 

如何travel(tree->left)travel(tree->right)得到由側端面上行駛的樹?

回答

2

我們應用的打印順序,然後向左再向右這樣:

A prints goes left 


    B prints goes left 
     D prints goes left 

     D back left goes right 

     D back right 
    B back left goes right 

    B back right 

    A back left goes right 

等等...

等你拿:

A B d C ^電子網