原理和图的广度优先遍历一样
算法描述如下
1.节点入队列
2.取队列首元素,访问之
3.如果左子树不为空,入队
4.如果右子树不为空,入队
5.队列是否为空?否,GOTO STEP 2
6.结束
关键代码:
void printtree(Node* bt,Que *que)
{
if(bt==NULL)
{
cout<<"空树"<<endl;
return ;
}
int depth=GetDep(bt);
Node *temp;
que->Putin(bt);
while(!que->IsEmpty())
{
temp=que->Out();
cout<<temp->val<<" ";
if(temp->GetLC()!=NULL)
que->Putin(temp->GetLC());
if(temp->GetRC()!=NULL)
que->Putin(temp->GetRC());
}
}结果:
3641

被折叠的 条评论
为什么被折叠?



