思路:
层序遍历 A.启动
B.拉下线
循环到队列彻底为空
1.把队首元素取出
2.打印
3.拉下线(左右孩子)
空树不要
代码如下:
#include<queue>
void levelOrder(Node *root){
if (root == NULL){
printf("\n");
}
//启动
std::queue<Node *> q;
q.push(root);
while (q.empty() != NULL){
Node *front = q.front();
q.pop();
printf("%c ", root->value);
if (front->left != NULL){
q.push(front->left);
}
if (front->right != NULL){
q.push(front->right);
}
}
printf("\n");
}