题目链接:https://www.acwing.com/problem/content/description/42/
题目如下:
/**
* 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>> result;
if(root==NULL) return result;
queue<TreeNode*> que;
que.push(root);
while(!que.empty()){
int size=que.size();
vector<int> temp;
while(size--){
TreeNode* node=que.front();
que.pop();
temp.push_back(node->val);
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
result.push_back(temp);
}
return result;
}
};