AcWing 43. 不分行从上往下打印二叉树
原题链接
简单
作者:
疾风劲草
,
2021-07-17 23:39:45
,
所有人可见
,
阅读 208
/**
* 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) {
vector<int>vec;
if(!root)
return vec;
queue<TreeNode *>que_;
que_.push(root);
while(!que_.empty())
{
int num=que_.size();
while(num--)
{
TreeNode *tmp=que_.front();
vec.push_back(tmp->val);
if(tmp->left)
que_.push(tmp->left);
if(tmp->right)
que_.push(tmp->right);
que_.pop();
}
}
return vec;
}
};