简单
题目描述
class Solution {
public:
vector<vector<int>> printFromTopToBottom(TreeNode* root) {
if (!root) return {};
vector<vector<int>> vec {};
queue<TreeNode*> q {{root}};
while(!q.empty()) {
auto size = q.size();
vector<int> vLevel(size);
for (size_t i = 0; i < size; i++) {
auto node = q.front(); q.pop();
vLevel[i] = node->val;
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
vec.emplace_back(move(vLevel));
}
return vec;
}
};