题解 | #把二叉树打印成多行#
把二叉树打印成多行
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>> ans;
queue<TreeNode*> que;
que.push(pRoot);
while (!que.empty()) {
vector<int> t;
for (int i = que.size() - 1; i >= 0; i--) {
auto f = que.front();
que.pop();
t.push_back(f->val);
if (f->left) que.push(f->left);
if (f->right) que.push(f->right);
}
ans.push_back(t);
}
return ans;
}
};