题解 | #把二叉树打印成多行#
把二叉树打印成多行
http://www.nowcoder.com/practice/445c44d982d04483b04a54f298796288
打印二叉树成多行
思路:简单的层序遍历
/*
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) {
if(!pRoot)return {};
vector<vector<int>> v;
queue<TreeNode*> q;
q.push(pRoot);
while(!q.empty()){
vector<int> t;
int size = q.size();
while(size--){
TreeNode* tmp = q.front();
q.pop();
t.push_back(tmp->val);
if(tmp->left)
q.push(tmp->left);
if(tmp->right)
q.push(tmp->right);
}
v.push_back(t);
}
return v;
}
};