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