题目描述
从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
class Solution {
public:
vector<vector<int> > Print(TreeNode* pRoot){
vector<vector<int> > vec;
if(pRoot==NULL) return vec;
queue<TreeNode*> q;
q.push(pRoot);
while(!q.empty()){
int len=q.size();
vector<int> temp;
for(int i=0;i<len;i++){
TreeNode *t=q.front();
if(t->left!=NULL)
q.push(t->left);
if(t->right!=NULL)
q.push(t->right);
temp.push_back(t->val);
q.pop();
}
vec.push_back(temp);
}
return vec;
}
};