/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
vector<int> PrintFromTopToBottom(TreeNode* root) {
//层序遍历 打印所有的值
//队列实现 先将root放入队列,判断队列是否为空 不为空打印
//再判断是否有孩子,一次将孩子从左到右入队列打印 后将root出战
queue<TreeNode*> q;
vector<int> v;
if (root != nullptr) {
q.push(root);
}
while (q.empty() == false) { //q不为空
v.push_back(q.front()->val);
if (q.front()->left != nullptr) {
q.push(q.front()->left);
}
if (q.front()->right != nullptr) {
q.push(q.front()->right);
}
//删除最先入队列的节点
q.pop();
}
return v;
}
};
标签:JZ32,TreeNode,打印,push,right,二叉树,front,root,left
From: https://www.cnblogs.com/H43724334/p/18146902