/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> printFromTopToBottom(TreeNode* root) {
queue<TreeNode*> q;
vector<int> r;
q.push(root);
while(!q.empty()){
TreeNode* f=q.front();
q.pop();
if(f!=NULL) r.push_back(f->val),q.push(f->left),q.push(f->right);
}
return r;
}
};
AcWing 43. 不分行从上往下打印二叉树
![](https://img-home.csdnimg.cn/images/20240711042549.png)