題目描述
從上到下按層打印二叉樹噪生,同一層結(jié)點從左至右輸出裆赵。每一層輸出一行
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;
}
};