void BreathSearchZZ(BianrySearchThree *tree){
if(tree == NULL){
return ;
}
std::queue<BianrySearchThree *> q;
q.push(tree);
int i = 0;
while(!q.empty()){
std::vector<int> temp;/*临时保存一层的数据*/
size_t size = q.size();
while(size--){/*这个循环就是把这一层的数据全部倒出来放在一个临时的数组中,方便后续打印*/
BianrySearchThree* node = q.front();
temp.push_back(node->data);
q.pop();
/*然后这一层以下的层次就自己该加入主队列就加入,该怎么加就怎么加*/
if(node->lChild){
q.push(node->lChild);
}
if(node->rChild){
q.push(node->rChild);
}
}
i++;
if((i % 2) == 0){/*如果偶数层顺序打印*/
for(auto &t:temp){
cout<<t<<" ";
}
cout<<endl;
}else {/*基数层反向打印,这里反向打印时候主要最大值取值(不包含)和最小值需要包含*/
for(vector<int>::iterator it=temp.end() - 1; it >= temp.begin();it--){
cout<<*it<<" ";
}
cout<<endl;
}
}
}
蛇型(Z型)打印二叉树节点信息-广度优先层次之间交替打印
最新推荐文章于 2023-04-11 13:57:44 发布