AcWing 43. 不分行从上往下打印二叉树
原题链接
简单
作者:
adamXu
,
2020-09-26 23:50:19
,
所有人可见
,
阅读 321
/**
* 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) {
//bfs
vector<int> res;
if(!root) return res;
queue<TreeNode*> q;
q.push(root);
while(q.size()){
auto x = q.front();
q.pop();
res.push_back(x->val);
if(x->left) q.push(x->left);
if(x->right) q.push(x->right);
}
return res;
}
};