把二叉树打印成多行(队列)
把二叉树打印成多行
http://www.nowcoder.com/questionTerminal/445c44d982d04483b04a54f298796288
/*
二叉树层次遍历
*/
class Solution {
public:
vector<vector<int> > Print(TreeNode* pRoot) {
vector<vector<int> > ans;
if(!pRoot) return ans;
queue<TreeNode*> q;
q.push(pRoot);
while(!q.empty()){
int size = q.size();
vector<int> res;
while(size--){
TreeNode *tmp = q.front();
q.pop();
res.push_back(tmp->val);
if(tmp->left)q.push(tmp->left);
if(tmp->right)q.push(tmp->right);
}
ans.push_back(res);
}
return ans;
}
};