从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
/*
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>> res;
if(pRoot==NULL) return res;
queue<TreeNode*> q;
q.push(pRoot);
while(!q.empty()){
vector<int> v;
int i=0,n=q.size();
while(i++<n){
TreeNode* tmp=q.front();
v.push_back(tmp->val);
q.pop();
if(tmp->left) q.push(tmp->left);
if(tmp->right) q.push(tmp->right);
}
res.push_back(v);
}
return res;
}
};