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