AcWing 43. 不分行从上往下打印二叉树
原题链接
简单
作者:
沙漠绿洲
,
2020-08-23 23:29:28
,
所有人可见
,
阅读 427
C++ 代码
/**
* 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) {
if(!root) return {};
queue<TreeNode*> q;
q.push(root);
vector<int> ret;
while(!q.empty()){
auto t = q.front();
q.pop();
ret.push_back(t->val);
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
return ret;
}
};