Hierarchical Traversal of Binary Tree

Hierarchical Traversal Output

tree *data[1000];

int tou=0,wei=0;
void print(tree *p)
{
data[tou]=p;
wei++;
while(tou!=wei)
{
cout<<data[tou]->data;
if(data[tou]->left !=NULL)
{
data[wei]=data[tou]->left;
wei++;
}
if(data[tou]->right !=NULL)
{
data[wei]=data[tou]->right ;
wei++;
}
tou++;
}

}

The idea of ​​stack is used here, starting from the head of the queue, and putting each node down at the tail of the queue until the head and tail of the queue are equal (the queue is empty)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324689191&siteId=291194637